Hi
I need to get IP address from linux server.
There are many script to do this, but no one is perfect.
They just grab eth0 or eth1 from ifconfig, and this may, may not be correct.
You may have several IF configured, but only one is default GW
Here is how I would like the process to be.
-
What is the default gateway?
This can be found usingroute -n
and look at flagsUG
-
Look at
ifconfig
and select the IP that correspond to default gateway within subnetmask.
This would give correct IP and ifname if you need it.
But there is but.
One one virtual server it may looks like this
route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 0.0.0.0 0.0.0.0 U 0 0 0 venet0
ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:12840125 errors:0 dropped:0 overruns:0 frame:0
TX packets:12840125 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:4483434938 (1.4 GB) TX bytes:4483434938 (1.4 GB)
venet0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:127.0.0.2 P-t-P:127.0.0.2 Bcast:0.0.0.0 Mask:255.255.255.255
inet6 addr: 2b00:daa0:da:99:225:71:fd32:d097/128 Scope:Global
UP BROADCAST POINTOPOINT RUNNING NOARP MTU:1500 Metric:1
RX packets:41235259 errors:0 dropped:0 overruns:0 frame:0
TX packets:39244451 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2827188617 (1.8 GB) TX bytes:3421181418 (1.4 GB)
venet0:0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:182.23.5.11 P-t-P:37.247.52.130 Bcast:0.0.0.0 Mask:255.255.255.255
UP BROADCAST POINTOPOINT RUNNING NOARP MTU:1500 Metric:1
No default gateway, and SM 255.255.255.255
This is typical on VPN tunnels etc.
So how do I find DG, IP and correct ifname??