nixCraft Poll

Topics

Read UNIX/Linux system IP address in a shell script

Posted by Vivek Gite [Last updated: January 16, 2006]

Reading an IP address in shell script required many time. However, different Linux distribution stores IP address in different files. If you are looking to run script under different UNIX like OSes such as Solaris or FreeBSD then you need to use ifconfig command. ifconfig not just use to configure a network interface but it can be use to obtained information such as network IP, netmask etc. Here are different command for different UNIX/Linux/BSD OSes in shell scripts to read IP address:

Linux:

ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' |
cut -d: -f2 | awk '{ print $1}'

FreeBSD/OpenBSD:

ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' |
awk '{ print $2}'

Solaris:

ifconfig -a | grep inet | grep -v '127.0.0.1' |
awk '{ print $2}'

See read-ip-address.bash script which reads/finds an IP address from diffrent UNIX/Linux OSes.

How it works?
Here you used something called pipeline. A pipeline is a set of processes chained by their standard streams, so that the output of each process ("stdout") feeds directly as input ("stdin") of the next one (see pipe at wikipedia for more information).

  1. ifconfig command list all network interfaces.
  2. From the output of ifcomfing command, find out IPv4 IP address using grep command (grep 'inet addr:').
  3. Next, you do not need loopback IP address (127.0.0.1) so again with the help of grep –v you invert the sense of matching, to select all non-matching lines.
  4. Finally, awk command selects an IP address. (awk '{print $2}')

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Anonymous Says:

    The process you listed would only give you the local address, if you are behind a firewall that uses NAT.

    To get your public IP address try out the following bash script that I wrote a while ago:

    #!/bin/bash

    wget whatismyip.org
    read T1 http://www.suramya.com

  2. nixcraft Says:

    Suramya,
    Script is ok, but you can do that with one line command:

    lynx –dump http://whatismyip.org/

    Or better create a alias:

    alias myip=’lynx –dump http://whatismyip.org/

  3. Anonymous Says:

    True, This script is actually a portion of another script that sends out an email when my public IP changes. I just copied the portion that actually gets the IP and pasted it here.

    That being said your version of the script is a lot cleaner. Mind if I ’steal’ it for use in my scripts? ;)
    Thanks,
    Suramya

  4. nixcraft Says:

    Mind if I ’steal’ it for use in my scripts? ;)

    Heh no problem, use it in your script :)

  5. Anonymous Says:

    @nixcraft. nice tip/hint :D I would like to see more shell scripting stuff here. keep it up good work

  6. Michael Gauthier Says:

    If you don’t have lynx installed and want to use wget without messy temporary files, you can use:

    wget -qO - http://whatismyip.org/

  7. Clue Says:

    Hi there,

    I’m new to all this and I was wondering, how do those websites get our IP and is there not a shell script that can be written to find out your public IP depending on your private IP without having to use these external websites?

  8. TJB Says:

    Dude, you just saved me 15 minutes of relearning awk and the like just to pull out the IP addy. Double thanks!

  9. lex Says:

    @Clue
    no. for as far as I know you’ll always need some external server to ’see’ who you are.
    But if someone knows another way to do this, the n please let me know.

  10. igor Says:

    if you use a different locale, consider this version for linux:

    LC_ALL=C ifconfig | grep ‘inet addr:’| grep -v ‘127.0.0.1′ | cut -d: -f2 | awk ‘{ print $1}’

  11. Oscar Says:

    @the suggestion of using whatsmyip.

    That is a great idea, but if you’re looking to find your ip address on a box that has no internet access then that would not work and the original script(s) should always work. If you’re on a private lan for example with no web access then you can’t see whatsmyip, or ipchicken, etc.

    This quick how-to is not so much on how to find your external IP address, but more on the actual address your system has been assigned either DHCP, or statically.

  12. Mohan Says:

    This should do the work

    ifconfig eth0| awk ‘NR==2 {print $2}’| awk -F: ‘{print $2}’

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.