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?
As @Alex mention
netstat -r
, if that's not available there is also another way:replace
eno1
with your device.dhclient -R routers
- I can't test it, as I don't havedhcp
set-up anywhere.For the
ppp0
device you can query the IP address of the peer withip addr show ppp0
.However, you don’t even need one, as for a point-to-point device like
ppp0
you can simply set the route only to the interface, omitting the router IP.This command will give you all the routes set in your Linux machine, including gateway:
For eth0:
for ppp0: