Is there an easy way to programmatically extract IP address?

Is there an easy way to programmatically extract IP address, without tedious parsing of ifconfig? I would not mind simple command output processing using sed to do it but not processing multiline files from /etc some place. What I am trying to do is modify my .bashrc to display the IP address of the host in the greeting message. I am using Ubuntu 12.04 but decided to post here instead of the Ubuntu forum because I consider this to be not distro-specific.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

NOTES

  1. NIC device handles

    The examples below assume that the network interface is a wireless card named wlan0. Adjust this bit in the examples for your particular situation. For example if it’s a wired NIC card, then it’s likely eth0.

  2. IPv4 – (Internet Protocol version 4)

    Also these examples are returning the IPv4 address. the “dotted quad” that most people identify as their “IP Address”.

    For example:

    inet addr:192.168.1.20  Bcast:192.168.1.255  Mask:255.255.255.0
    
  3. IPv6 – (Internet Protocol version 6)

    If your system is configured to support IPv6 you’ll see both the “dotted quad” as well as the IPv6 IP addresses in the ifconfig output.

    For example:

    inet6 addr: fe80::226:c7ff:fe85:a720/64 Scope:Link
    

    The commands below explicitly ignore this, but could be adapted quite easily to grab this information instead.

Solutions (using ifconfig)

There are many ways to do this. You could for example use this awk script to parse out the IP address of your wireless LAN NIC (wlan0):

$ ifconfig wlan0 | grep "inet " | awk -F'[: ]+' '{ print $4 }'
192.168.1.20

You can do this and make it more compact:

$ ifconfig wlan0 | awk '/t addr:/{gsub(/.*:/,"",$2);print$2}'
192.168.1.20

You could also do it using perl:

$ ifconfig wlan0 | perl -nle'/t addr:(\S+)/&&print$1'
192.168.1.20

The Perl example is about as compact as it can get.

There are countless other ways to do this, these are just a couple of examples to get you started.

EDIT #1

Some additional notes and comments. @StephaneChazelas demonstrated that there is an even more compact version using grep:

$ ifconfig wlan0|grep -Po 't addr:\K[\d.]+'
192.168.1.20

This solution makes use of grep‘s ability in newer versions to make use of PCRE (Perl regular expressions), along with it’s -o switch to return just what matches the regular expression.

Solutions (using ip)

As was also mentioned in the comments, ifconfig can be troublesome to use on systems that have multiple IP addresses assigned to a network device, given it only returns the first one. So it’s better to use the command ip in these situations.

For example:

$ ip addr show wlan0 | grep -Po 'inet \K[\d.]+'
192.168.1.20

You can also tell ip to only show you IPv4 information for a given network interface. In this case we’re looking only at the interface named wlan0:

$ ip -f inet addr show wlan0 | grep -Po 'inet \K[\d.]+'
192.168.1.20

References

Solution 2

$ hostname -I

For example:

$ hostname -I
192.168.1.18

Info. from manpages:

http://manpages.ubuntu.com/manpages/raring/en/man1/hostname.1.html
http://unixhelp.ed.ac.uk/CGI/man-cgi?hostname

Solution 3

Here is another one using “ip” but this works better when your device might be connected with different interfaces, such as wired Ethernet sometimes and WiFi other times. I also used “sed” instead of “grep” or “perl”, for variety.

This finds whatever source IP has a route to the Internet. Or to Google’s DNS at any rate.

ip -o route get 8.8.8.8 | sed -e 's/^.* src \([^ ]*\) .*$/\1/'

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply