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:
Re: Handy one-liner: get current IP address on Linux
thanks!!
posted by Anonymous on Apr 1, 2007 2:46 AM
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!!
posted by Sean on Apr 5, 2007 6:11 AM
Re: Handy one-liner: get current IP address on Linux
obligatory fine-tuning:
/sbin/ifconfig eth0 | perl -ne 'print "$1\n" if /addr[\d\.]+)/'
posted by Anonymous on May 6, 2007 4:17 PM
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:
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
posted by Adrian Suter on Jul 26, 2007 11:15 AM
Re: Handy one-liner: get current IP address on Linux
thanks for the hint. remember the diff
* ifconfig on Linux
* ipconfig on windows.
posted by Jens611 on Sep 26, 2007 11:03 AM
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:
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}'
posted by Brian Bay on Sep 15, 2008 7:01 PM
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 ?
posted by Anonymous on Sep 16, 2008 3:41 PM
Re: Handy one-liner: get current IP address on Linux
Actually any of the above examples will work with the following