Navigation

RSS 2.0 New Entries Syndication Feed Atom 0.3 New Entries Syndication Feed

Show blog menu v

 

General

Use it

Documentation

Support

Sibling projects

RIFE powered

Valid XHTML 1.0 Transitional

Valid CSS!

Blogs : Archives

avatar
< Reindeer Arm Wrestling   My new ride : Kawasaki ER-6f (Ninja 650r) Metallic Diablo Black >
Handy one-liner: get current IP address on Linux

This took me a little bit longer that I'd hoped, I tried with awk first but finally settled on perl. Maybe this could be handy for someone else if you need to have the current IP address of eth0 isolated for shell scripting:

ifconfig eth0 | perl -n -e 'if (m/inet addr:([\d\.]+)/g) { print $1 }'

Could result into:

216.182.233.220
posted by Geert Bevin in IT on Jan 18, 2007 8:11 PM : 10 comments [permalink]
 

Comments

Re: Handy one-liner: get current IP address on Linux
thanks!!
Re: Handy one-liner: get current IP address on Linux
Most cool. I am setting up an off-site backup, and this lets me make things like the qmail ip address dependent upon the actual configuration of the server. Thanks!!
Re: Handy one-liner: get current IP address on Linux
obligatory fine-tuning:

/sbin/ifconfig eth0 | perl -ne 'print "$1\n" if /addr:([\d\.]+)/'

:)
Re: Handy one-liner: get current IP address on Linux
Remark: If your system language is not english, these one-liners wouldn't work. For example, here is the output of "ifconfig eth1" in german:

eth1 Protokoll:Ethernet Hardware Adresse 00:0E:35:12:4A:41
inet Adresse:192.168.0.122 Bcast:192.168.0.255 Maske:255.255.255.0
inet6 Adresse: fe80::20e:35ff:fe12:4a41/64 Gültigkeitsbereich:Verbindung

You see, there is no "addr:" but a "Adresse:" instead. So don't use this one-liner in a program that should run in international companies. Maybe you could modify the regexp such that it searches for the "inet " followed by anything and a ":". Here it is

/sbin/ifconfig eth1 | perl -ne 'print "$1\n" if /inet .*:([\d\.]+)/'

Possibly "inet" would not change in any language.

All the best
Re: Handy one-liner: get current IP address on Linux
thanks for the hint. remember the diff
* ifconfig on Linux
* ipconfig on windows.
Re: Handy one-liner: get current IP address on Linux
<<Oops, had a typo in the first post. This one is better>>

Here are two other one-liners which should be language independent:

ifconfig eth0 | sed -n 2p | cut -d ":" -f2 | cut -d " " -f1

This gives the value delimited by a colon ( : ) on the left and blank on the right which is on the second line of the output.

With sed only based on the same idea but with a much clumsier syntax:

ifconfig eth0 | sed -n "2s/[^:]*:[ \t]*\([^ ]*\) .*/\1/p"

Marty
Re: Handy one-liner: get current IP address on Linux
ifconfig eth0 | grep "inet addr" | awk 'sub(/addr:/,""){print $2}'
Re: Handy one-liner: get current IP address on Linux
Sorry about the repost. Here is the command without the garbage. I didn't see anyone else using awk, so I posted. ifconfig eth0 | grep "inet addr" | awk 'sub(/addr:/,""){print $2}'
Re: Handy one-liner: get current IP address on Linux
Any chance of translating one of those into a variable to be used in a script ?
Re: Handy one-liner: get current IP address on Linux
Actually any of the above examples will work with the following

ETH0=$(ifconfig eth0 | sed -n "2s/[^:]*:[ \t]*\([^ ]*\) .*/\1/p")
echo $ETH0

Add a new comment

:) ;)
=) :-)
:'( :(
:/ :D
:| :p
:o 8)
Your email address will not be displayed at anytime on any page.
Only provide your email address if you'd like updates on this entry
and it's comments by email.
Please answer this simple math question:
17 - 12 = 
 
 
  

Manage subscription

Remove email:
 

< Reindeer Arm Wrestling   My new ride : Kawasaki ER-6f (Ninja 650r) Metallic Diablo Black >
 
 
 
Google
rifers.org web