Linux: Parse an IP Address

by on January 28, 2010 · 0 comments· LAST UPDATED January 28, 2011

in

How do I parse my Linux servers IP address using a Bash shell?

You need to use the ifconfig or ip command to get ip address under Linux.

Parse an IP Address with ifconfig Command

Open a command-line terminal (select Applications > Accessories > Terminal), and then type the following commands:
# ifconfig eth0 | grep 'inet addr:'
Sample outputs:

          inet addr:192.168.2.100  Bcast:192.168.2.255  Mask:255.255.255.0

To get only an IP address, Bcast and Mask, use the egrep command as follows:
# ifconfig eth0 | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}'
Sample outputs:

192.168.2.100
192.168.2.255
255.255.255.0

Parse an IP Address with ip Command

Use the ip command as follows:
# ip -f inet addr show eth0| grep 'inet'
Sample outputs:

inet 192.168.2.100/24 brd 192.168.2.255 scope global eth0

Just display an IP:
# ip -f inet addr show eth0 | awk -F'inet' '{ print $2}' | cut -d' ' -f21

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , , , , , , , , , , ,

Previous Faq:

Next Faq: