[RndTbl] determine IP address in linux

Trevor Cordes trevor at tecnopolis.ca
Sat Mar 23 22:40:33 CDT 2013


Seems simple.  What's the best way (in a script) to get the IP address of 
an ethernet interface (in linux).  For instance eth0.

I used to have (perl, but also applies as a bash solution):

$eip=`/sbin/ifconfig $int | grep 'inet addr' | awk '{print \$2}' | sed -e 's/.*://'`

But the latest upgrade from F16 to F17 broke this (now the line contains 
"inet" but not "inet addr".

So I started thinking of finding the most standardized way that 
(hopefully) won't change after a future kernel upgrade :-)

It would seem that the less field parsing done, the better, as keywords 
and field position aren't guaranteed.

Looks like ifconfig is now deprecated (according to man ifconfig).  So now 
I'm doing:

$eip=`ip -o -4 addr list eth0 | awk '{print \$4}'`

That works now, but it still has a output format-dependent requirements.

I looked for /sys or /dev files, but can't find any that have ip4 addr.  I 
tried to find more options to whittle ip's output to give me just the 
address, no joy there.

Ideas?

PS: this is important as failures like this force me to drive out onsite 
to headless boxes, some of which are 200km away.


More information about the Roundtable mailing list