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).
- ifconfig command list all network interfaces.
- From the output of ifcomfing command, find out IPv4 IP address using grep command (grep 'inet addr:').
- 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.
- Finally, awk command selects an IP address. (awk '{print $2}')
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- 10 Greatest Open Source Software Of 2009
- My 10 UNIX Command Line Mistakes
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
- Email this to a friend
- Download PDF version
- Printable version
- Comment RSS feed
- Last Updated: Jan/16/2006


{ 1 trackback }
{ 18 comments… read them below or add one }
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
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/‘
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
Mind if I ’steal’ it for use in my scripts? ;)
Heh no problem, use it in your script :)
@nixcraft. nice tip/hint :D I would like to see more shell scripting stuff here. keep it up good work
If you don’t have lynx installed and want to use wget without messy temporary files, you can use:
wget -qO – http://whatismyip.org/
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?
Dude, you just saved me 15 minutes of relearning awk and the like just to pull out the IP addy. Double thanks!
@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.
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}’
@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.
This should do the work
ifconfig eth0| awk ‘NR==2 {print $2}’| awk -F: ‘{print $2}’
curl whatismyip.orgdoes the same too. No need of extra parametres. :-)My current favourite:
ifconfig | sed -n -e 's/:127\.0\.0\.1 //g' -e 's/ *inet addr:\([0-9.]\+\).*/\1/gp';)
@mohan, you can have multiple separators and not have to rerun awk.
@paul, do you like yours because it has a sed abomination in it? Make it simple!
ifconfig eth0 | awk -F”[: ]+” ‘NR==2 {print $4}’
Thanks! My virtual Ubuntu server is having problems keeping the same IP from DHCP. I need it to start bind my Django app for whole network so this tip came in handy.
On linux:
hostname -i
OMFG, guys, please, you *do* realize anybody can hack into whatismyip.org and INSTANTLY gain access to your boxes ?!!!! And you’re not even using HTTPS – so any monkey in the middle can 0wn your boxes too !!!!
Are you dumb enough to run those lynx/curl scripts as root as well ???
NEVER EVER put this much trust into 3rd party systems, or you WILL get screwed !
it would be funny watching you after whatismyip.org returns `rm -rf /`
use ifconfig / ip show… and don’t be so freaking lazy