0

I'm writing a script to control my network interfaces. I've three network interfaces viz eth0, ppp0, wlan0. I want such a configuration that when there is no internet available on eth0, it should switch to ppp0 and when ppp0 is also down, it should switch to wlan0.

Whenever I need to switch from eth0 to ppp0, I write following command:

sudo route add default gw "IP of ppp0"

And it gets connected. But the problem is there while switching between eth0 to wlan0 and vice-versa.

Sometimes while using sudo route add default gw 192.168.1.254 eth0, I get following response:

SIOCADDRT: File exists

And interface do not switch.

I check for the route command and I got following result:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.254   0.0.0.0         UG    202    0        0 eth0
default         192.168.43.141  0.0.0.0         UG    303    0        0 wlan0
10.64.64.64     0.0.0.0         255.255.255.255 UH    304    0        0 ppp0
192.168.0.0     0.0.0.0         255.255.252.0   U     202    0        0 eth0
192.168.43.0    0.0.0.0         255.255.255.0   U     303    0        0 wlan0

Then I've to remove all interfaces by using following command multiple times:

sudo route del default

Then after checking route again, I successfully deleted existing gateways and get this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.64.64.64     0.0.0.0         255.255.255.255 UH    304    0        0 ppp0
192.168.0.0     0.0.0.0         255.255.252.0   U     202    0        0 eth0
192.168.43.0    0.0.0.0         255.255.255.0   U     303    0        0 wlan0

But the problem I faced is that, after 4-5 minutes, these gateways appears again without any changes made by me. Help me to make me understand the working of linux here. How gateway IPs are configured automatically and how can I prevent it to do so.

5
  • What you are (probably) trying is an exercise in futility - although you haven't said what you are doing. /etc/resolv.conf contains # Generated by resolvconf - this is normal for virtually all dhcp clients. You can change it but it will be overwritten! What you (probably) want can be done with dhcpcd - see How to set up networking/WiFi NOTE having 2 default routes is a recipe for disaster!
    – Milliways
    Dec 17, 2019 at 5:32
  • One of my question is, after deleting the gateway, how they're configuring again after few minutes? Dec 17, 2019 at 5:34
  • because it is automatically Generated by resolvconf
    – Milliways
    Dec 17, 2019 at 5:36
  • Ok. I'll try what you told. Dec 17, 2019 at 5:39
  • You should not reinvent the wheel. What you try is a typical dynamic failover and that is managed with bonding. You can find an example with two interfaces at Howto migrate from networking to systemd-networkd with dynamic failover. But it is no problem to have more interfaces with bonding.
    – Ingo
    Dec 18, 2019 at 10:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.