[SOLVED] Easy way to automatically switch between two network adapters depending on which of the two has internet access
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'd responded to you thinking you were using a couple of dial-up modems. Editing my post.
Yes, if you are connected to a DSL router via Ethernet, then the link state would be the same.
It might be useful if you explain to us exactly what your setup is, rather than us guessing what you are using.
You had initially written:
Quote:
I have two network adapters connecting to different isp's.
and now you are telling us you are using a DSL router?
Right.
So write yourself a script which runs in a loop, testing connectivity of remote resources (such as 8.8.8.8), and modifies the default route depending on the connectivity.
That shouldn't be so hard. The information you require is already in this thread.
That gc_timeout setting above seems to do just what I want automatically according to the docs. But what should the routing table look like for this to work?
Made a script in the end. The way to switch the default route is like this:
To switch to eth1:
ip route change default via 192.168.1.1 dev eth1
To switch back to eth0:
ip route change default via 192.168.0.1 dev eth0
So this script is quite robust, no matter which isp fails it will do the right thing to get back online.
Code:
#!/bin/sh
PINGTARGET=8.8.8.8
PINGINTERVAL=3
PINGWAITFORRESPONSE=3
while [ 1 ]
do
echo Waiting for ping to fail
while [ 1 ]
do
ping -c 1 -W $PINGWAITFORRESPONSE $PINGTARGET >/dev/null 2>/dev/null || break
sleep $PINGINTERVAL
done
echo Switching to eth1
ip route change default via 192.168.1.1 dev eth1 2>/dev/null
echo Waiting for ping to fail
while [ 1 ]
do
ping -c 1 -W $PINGWAITFORRESPONSE $PINGTARGET >/dev/null 2>/dev/null || break
sleep $PINGINTERVAL
done
echo Switching to eth0
ip route change default via 192.168.0.1 dev eth0 2> /dev/null
sleep 1
done
It would be nice if it switched back to eth0 when this isp is back online as this isp is faster. But how can a script test this without disrupting your connectivity while using a browser?
You could setup a static route for 8.8.8.8/32 pointed to eth0 and 8.8.4.4/32 for eth1.
Then you can ping 8.8.8.8 for ISP1 test, and 8.8.4.4 for ISP2, regardless of where the default route is pointing at the time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.