I want to make big script on my Debian 7.3 ( something like translated and much more new user friendly enviroment ). I have a problem. I want to use only some of the informa
Just a note, since I just spent some time trouble-shooting a botched upgrade on a server.
Turned out, that (years ago) I had implemented a test to see if dynamically added interfaces (e.g. eth0:1) were present, and if so, I would bind certain proggis to the 'main' IP on eth0. Basically it was a variation on the 'ifconfig|grep...|sed... ' solution (plus checking for 'eth0:' presence).
The upgrade brought new net-tools, and with it the output has changed slightly:
old ifconfig:
eth0 Link encap:Ethernet HWaddr 42:01:0A:F0:B0:1D
inet addr:10.240.176.29 Bcast:10.240.176.29 Mask:255.255.255.255
UP BROADCAST RUNNING MULTICAST MTU:1460 Metric:1
...
whereas the new version will display this:
eth0: flags=4163 mtu 1460
inet 10.240.212.165 netmask 255.255.255.255 broadcast 10.240.212.165
...
rendering the hunt for 'eth0:' as well as 'inet addr:' search busted (never mind interfaces called 'em0','br0' or 'wlan0'...). Sure you could check for 'inet ' (or 'inet6'), and make the addr: part optional, but looking closer, you'll see that more or less everything has changed, 'Mask' is now 'netmask',...
The 'ip route ...' suggestion's pretty nifty - so maybe:
_MyIP="$( ip route get 8.8.8.8 | awk 'NR==1 {print $NF}' )"
if [ "A$_MyIP" == "A" ]
then
_MyIPs="$( hostname -I )"
for _MyIP in "$_MyIPs"
do
echo "Found IP: \"$_MyIP\""
done
else
echo "Found IP: $_MyIP"
fi
Well, something of that sort anyway. Since all proposed solutions seem to have circumstances where they fail, check for possible edge cases - no eth, multiple eth's & lo's, when would 'hostname -i' fail,... and then decide on best solution, check it worked, otherwise 2nd best.
Cheers 'n' beers!