How to find out linux gateway IP address

dynamic-ipgatewayifconfiglinux-networkingroute

I have CentOS 6.7 with two Internet connections

eth0 – dynamic IP address and dynamic gateway via DHCP (controlled by NetworkManager)

ppp0 – dynamic IP address and dynamic gateway (configured with rp_pppoe)

I want write script which will change the default gateway on in some conditions something like this:

ip route replace default scope global via $IP1 dev eth0
# or
ip route replace default scope global via $IP2 dev ppp0
# or
ip route replace default scope global nexthop via $IP1 dev eth0 weight 8 nexthop via $IP2 dev ppp0 weight 10

How to find out dynamic gateway $IP1 and $IP2?

update:

from this answer https://unix.stackexchange.com/a/124341/157086

in file /var/lib/dhclient/dhclient.leases I can find gateway to eth0

option routers 12.34.59.28;

How to find out dynamic gateway $IP2 for ppp0?

Best Answer

As @Alex mention netstat -r, if that's not available there is also another way:

$ ip route list dev eno1
default via X.X.X.X  proto static  metric 100 
X.X.X.X/24  proto kernel  scope link  src X.X.X.X  metric 100 
$ 

replace eno1 with your device.

dhclient -R routers - I can't test it, as I don't have dhcp set-up anywhere.

Related Topic