1

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?

snex
  • 191
  • 2
  • 8

4 Answers4

1

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.

alexus
  • 13,112
  • 32
  • 117
  • 174
1

For the ppp0 device you can query the IP address of the peer with ip 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.

Stefan Paletta
  • 231
  • 1
  • 3
0

This command will give you all the routes set in your Linux machine, including gateway:

route -n
sysfiend
  • 1,387
  • 1
  • 12
  • 24
0

For eth0:

#!/bin/bash    
gwip=$(nmcli dev list iface eth0 | grep IP4-SETTINGS.GATEWAY: | awk '{ print $2}')
ip route replace default scope global via $gwip dev eth0

for ppp0:

#!/bin/bash 
gwip2=$(/sbin/ip addr show ppp0 | grep peer | awk ' { print $4 } ' | sed 's/\/32//')
ip route replace default via $gwip2 dev ppp0
snex
  • 191
  • 2
  • 8