my_public_ip=`curl http://ifconfig.me`


On Sat, Mar 23, 2013 at 10:40 PM, Trevor Cordes <trevor@tecnopolis.ca> wrote:
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.
_______________________________________________
Roundtable mailing list
Roundtable@muug.mb.ca
http://www.muug.mb.ca/mailman/listinfo/roundtable



--
Sean Walberg <sean@ertw.com>    http://ertw.com/