How to grep and display IP Address on Linux

Posted by Planet Malaysia on April 9, 2009

advertisement

How to find out an IP Address on Linux is pretty simple by type “ifconfig” command.

Output:

# ifconfig
eth0 Link encap:Ethernet HWaddr 01:2A:A1:3C:C3:27
inet addr:192.168.1.23 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:256125364 errors:0 dropped:0 overruns:0 frame:0
TX packets:203700834 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:240597045 (229.4 Mb) TX bytes:3399069596 (3241.6 Mb)
Interrupt:16 Memory:f8000000-f8011100

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:4760967 errors:0 dropped:0 overruns:0 frame:0
TX packets:4760967 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:480898269 (458.6 Mb) TX bytes:480898269 (458.6 Mb)

For some reason, you may want to display IP Address only. You can do it on multiple way and here you go:

Example 1:
#ifconfig eth0 | grep ‘inet addr:’ | cut -d: -f2
192.168.1.1 Bcast

Example 2:
#ifconfig | awk ‘/^eth/{s=$1;getline;print s,$1,$2}’
eth1 inet addr:192.168.1.1

Example 3:
#ifconfig eth0 | grep ‘inet addr:’ | cut -d: -f2 | awk ‘{ print $1}’
192.168.1.1

Example 4:
#ip addr list eth1 |grep inet |cut -d’ ‘ -f6|cut -d/ -f1
192.168.1.1

Example 5 - IPV6:
#ip addr list eth0 |grep “inet6 ” |cut -d’ ‘ -f6|cut -d/ -f1

Example 6:
#hostname -i
192.168.1.1

Possibly Related Posts:


Comments

Leave a Reply




Planet Malaysia

  • Follow us on Twitter


  • web www.planetmy.com