[olug] Unix Tip: GET YOUR IP-ADDRESS (long explanation)

Eric Penne epenne at olug.org
Thu Mar 20 20:11:41 UTC 2003


ifconfig | grep "inet addr" |  grep -v "127.0.0.1" | awk '{print $2;}' |
awk -F':' '{print $2;}'


Here is a rundown on this command for the newbies.

1) ifconfig is usually located in /sbin/ifconfig shows the network
interfaces that are up and how they are configured.

2) grep pulls lines out of the ifconfig info that contain inet addr

3) grep -v inverts.  It prints all the lines that don't have 127.0.0.1
in them.

4) awk looks at the info and pulls the second field out, where the
fields are separated by whitespace by default.

5) Then awk -F':' is used again to pull the second field out, where the
fields are separated by a colon.

The individual outputs follow:

1)

eth0      Link encap:Ethernet  HWaddr 00:80:AD:0C:BA:43
          inet addr:192.168.1.42  Bcast:192.168.255.255
Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST  MTU:1500
Metric:1
          RX packets:34866 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5882 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:7133322 (6.8 MiB)  TX bytes:850473 (830.5 KiB)
          Interrupt:3 Base address:0xd800

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:400 (400.0 b)  TX bytes:400 (400.0 b)

2)
          inet addr:192.168.1.42  Bcast:192.168.255.255
Mask:255.255.0.0 inet addr:127.0.0.1  Mask:255.0.0.0

3)
          inet addr:192.168.1.42  Bcast:192.168.255.255
Mask:255.255.0.0

4)
addr:192.168.1.42

5)
192.168.1.42


Now don't we all love the pipe command.  Hopefully if you are having
trouble with the above command this explanation can step you through the
issues.

Eric





More information about the OLUG mailing list