//View Tip #554
Similar Tips
» BBC Weather
» Random password generator.
» Remove every file but one
» Check memory and swap from command line
» Grepping for processes

 

Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
You sometimes need your machines IP adress without relying on external sources; so here is the command to return your IP adress in one neat line.

ifconfig eth0 | grep -o "addr:[0-9.]*" | grep -o "[0-9.]*"


You off course need the necessary rights to us this. If you lack the rights, you might want to prepend it with "sudo" and add the ifconfig to the allowed paswordless commands for the user that needs this..

Ifconfig retrieves the adress; the first grep weeds out the external adress out of the data & the last grep filters out the IP adress.


View Comments »




Comments 

Add your comment

Comments are currently disabled
Hi,

I must add a $ at the end of the second grep to work:

/sbin/ifconfig eth0 | grep -o "addr:[0-9.]*" | grep -o "[0-9.]*$"


Also added /sbin/ to work in user mode, not necesary sudo, tested in Debian Etch.
Posted 2009-03-08 17:39:12
Hi,
My active eth interface is eth1 not eth0 (eth0 is for my wired connection, eth1 is for my wireless connection).

Anyway, the IP address you get is your *internal* IP, i.e. the IP assigned for you in your LAN (for eg. if you're behind a NAT, you'll most likely see something like 192.168.bla.bla)
If you want your external IP, I guess you need an external "observer" to tell it to you, because your computer is not aware of it.

To get your "real" (i.e. external) IP, you can query whatismyip.com with this command:

wget -O - whatismyip.com 2> /dev/null | grep "Your IP Address Is" | sed -e 's/^.*Is//' -e 's/<.*$//'
Posted 2009-03-08 23:30:18
It's quite similar on OS X. I'm on a WiFi connection right now, so my active interface is en1.

ifconfig en1 | grep -o "inet [0-9.]*" | grep -o "[0-9.]*$"
Posted 2009-03-09 05:41:17
Simpler:
ifconfig | sed -rn 's/.*r:([^ ]+) .*/\1/p' # list IPs
ifconfig eth0 | sed -rn 's/.*r:([^ ]+) .*/\1/p' # list eth0 IP
Posted 2009-03-10 17:11:46

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit