You are here

Switch between default gateways to switch ISPs

Submitted by Druss on Mon, 2018-11-26 21:32

So I have a couple of routers connected to my network each linking to a different ISP. When an ISP goes down, I want to easily switch ISPs from the command line and these are the couple of lines I use to do so:


sudo ip route del default

if [ $1 == 'isp1' ]
  echo "Switching ISP to $1"
  sudo ip route add default via
  echo "Switch ISP to ISP2"
  sudo ip route add default via

I put this into a file (say .isp), give it execute permissions, and then type ./.isp isp1 to switch to ISP 1. Typing anything else changes to ISP 2.

There is an ip route change default option as well. But this is preferable as I would occasionally run into errors similar to RTNETLINK answers: No such file or directory otherwise.

You can add the following code to display your current external IP and test your connection with a ping:

dig TXT +short
ping -n -c 4

( is one of Google's public DNS servers.)

Hope this helps someone.