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:

#!/bin/bash

sudo ip route del default

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

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 o-o.myaddr.l.google.com @ns1.google.com
ping -n -c 4 8.8.8.8

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

Hope this helps someone.