script로 ip 알아 내는 방법?

dreamer의 이미지

script로 ip를 알아 내는 방법은 무엇인가요?

저는 ifconfig를 써서 알아 내려고 하는데, 더 좋은 방법은?
그런데 ifconfig로 하면

ifconfig eth0 | grep "inet addr"
inet addr:192.168.0.201  Bcast:192.168.0.255  Mask:255.255.255.0

처럼 되는데, 여기서 192.168.0.201만 뽑아 내고 싶습니다.
cut을 사용할려고 하니 생각 처럼 잘 안되네요..
혹 좋은 방법이 있으시면..
dakiller6의 이미지

ifconfig rl0 | grep "inet " | cut -d '\t' -f 2

cut 뒤에 오는 -d를 이용해서 구분자를 나타내줍니다.
-f 를 이용해서 이 구분자로 구분된 필드 중 몇 번째 필드를 원하는 써주면 됩니다.

전 리눅스가 아닌 FreeBSD에서 해보면

clouddata[~]$ ifconfig rl0
rl0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        inet 192.168.41.41 netmask 0xffffff00 broadcast 192.168.41.255
        inet6 fe80::208:a1ff:fe6d:bfa4%rl0 prefixlen 64 scopeid 0x1
        ether 00:08:a1:6d:bf:a4
        media: Ethernet autoselect (100baseTX <full-duplex>)
        status: active

이 결과물이기 때문에,
ifconfig rl0 | grep "inet " | cut -d ' ' -f 2

라고 치면 IP가 나옵니다. 구분자를 탭[\t]에서 공백으로 바꾸었습니다.

님의 경우 cut을 사용한다면

ifconfig rl0 | grep "inet " | cut -d ':' -f 2 | cut -d '  ' -f 1

이라고 해야할 꺼 같네요. 즉 :를 구분자로 해서 "192.168.0.201 Bcast"를 추출해내고 이를 다시 공백 두칸으로 구분해서 첫번째 것만 뽑아내는 방식으로 말이예요

만약에 콘솔이 아닌 텔넷이나 SSH 접속이라면
ECHO $REMOTEHOST 하시면 알 수 있습니다. (저는 tcsh이라서 bash에서는 변수명이 다를수도 있겠네요)

정태영의 이미지

Quote:
root # ifconfig eth0 |grep "inet addr"|awk '{ print $2 }'|awk -F: '{ print $2 }'
xx.xxx.xxx.xxx

만만한게 awk 라고 =3=33

오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

mudori의 이미지

hostname -i

정태영의 이미지

dakiller6 wrote:
만약에 콘솔이 아닌 텔넷이나 SSH 접속이라면
ECHO $REMOTEHOST 하시면 알 수 있습니다. (저는 tcsh이라서 bash에서는 변수명이 다를수도 있겠네요)

어떤 ssh 클라언트를 쓰시나요?
전 그런 환경변수가 없군요 :)

그리고 remotehost 라면.. 클라이언트의 아이피가 아닐까 싶군요 :)

오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

정태영의 이미지

mudori wrote:
hostname -i

앗 전.. 127.0.0.1 이라는군요 :evil:

Quote:
root # hostname -i
127.0.0.1

오랫동안 꿈을 그리는 사람은 그 꿈을 닮아간다...

http://mytears.org ~(~_~)~
나 한줄기 바람처럼..

dreamer의 이미지

진짜 빠른 답변이군요.. 나이스 고맙습니다.
저도 답을 구하기는 했네요..

/sbin/ifconfig $DEVICE | grep "inet addr" | awk '{print  $2}'| cut -f2 -d":

hostname -i

나이스군요..

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.