gerardo.gomez wrote:To sum up, IP values in /etc/hosts and /etc/network/interfaces have to be same value!
No they don't, and you can cause some really fun to diagnose issues with sudo and ssh playing with the hosts file.
I just strung this together, so there is probably a cleaner way to do it, but this will get the ip address of the interface that has the default route attached to it. It works on GNU ifconfig, BSD ifconfig (like OS X) is a little different in layout but some tweaks would make it work there also.
Just pass this to application.executeProgram(), the result will be your IP address.
- Code: Select all
netstat -rn| grep "^0.0.0.0" | awk '{print $8}' | xargs -n1 ifconfig |grep "inet addr"| sed 's/addr://'|awk '{print $2}'
To break down what it is doing...
netstat -rn : prints out your routing table without looking up dns names
grep "^0.0.0.0" : finds the line that has the default route
awk '{print $8}' : grabs the 8th column which is the interface name
xargs -n1 ifconfig : passes that interface name to ifconfig
grep "inet addr" : find the line that has the IPv4 address
sed 's/addr://' : strip out the addr: to make the following awk easy
awk '{print $2}' : grab the second column, the ip address.