Using a script to get your IP address
Posted 01-07-2013 at 11:33 AM by sag47
A friend of mine was complaining today how their work place doesn't parse the IP address from ifconfig but asks for manual input from the user. This is the dumbest thing ever. In case you're ever encountered by this here's a horrible one liner to get the job done quick and dirty.
Also, you may need to replace eth2 with your correct interface (such as eth1).
It works, move along.
Also, you may need to replace eth2 with your correct interface (such as eth1).
Code:
ifconfig | sed -ne '/^eth2/,/^\s\+Interrupt/p' | awk '$0 ~ /inet addr/ { print $2 }' | cut -d: -f2
Total Comments 2
Comments
-
Save two commands, get more awk for free:
Code:/sbin/ifconfig | awk '/inet addr:/ {if ($2 !~ "127.*") print substr($2,6)}'
Posted 01-07-2013 at 06:32 PM by unSpawn -
Thanks . I was discussing it with my local LUG mailing list and here's a good solution they came up with which will present the IP address of the default route (I substituted some of their cuts with awk instead).
Code:ip -o ro get $(ip ro | awk '$1 == "default" { print $3 }') | awk '{print $5}'
Code:sed -ne '/^eth2/,/^\s\+Interrupt/p'
Posted 01-08-2013 at 04:35 PM by sag47