I have written a quick&dirty bash script to check which gateway is still working and switch to it. It also switches back to the primary gateway as soon as it is available again. The script can be called on a regular basis via cron.
#!/bin/bash ## Create logfile and send all output to logfile logfile=/var/log/gateway-check.log #exec &> >(tee -a "$logfile") ## Define variable for a date/time stamp datetimestamp=$(date) ## Define variable for destination MACs ## Define variable for the source IP to use for the ping source GW_NAME=(Internet Alternative SomethingElse) GW_MAC=(00:11:22:33:44:55 00:11:22:33:44:66 00:11:22:33:44:77) GW_IP=(192.168.1.1 192.168.2.1 192.168.3.1) SRC_IP=(192.168.1.45 192.168.2.45 192.168.3.45) ## Define variable for ping target PING_TARGET=8.8.8.8 ## Define email fields MAIL_TO=me@example.com ######### ## Logic is: ## check if target is reachable ## NO -> search a GW and switch to it ## YES -> check if GW is the default GW (the first one in the array) ## YES -> exit ## NO -> check if default GW can reach internet. ## YES -> switch to default GW ## NO -> keep the current GW ######### ## check if target is reachable pingTarget=$(nping --icmp --icmp-type echo $PING_TARGET | grep Lost | awk '{print $12}') gateway=$(route -n | grep ^0.0.0.0 | awk '{print $2}') if [ $pingTarget -le 5 ]; then ## target reachable ## Check if current route is via first GW and works if [ "$gateway" = "${GW_IP[0]}" ]; then ## we ware using default GW, all is fine echo "***** "$datetimestamp" - GW OK. Packet loss: "$pingTarget"/5" >> "$logfile" exit 0 else gwReachesNet=$(nping --icmp --icmp-type echo --source-ip ${SRC_IP[0]} --dest-mac ${GW_MAC[0]} --delay 500ms $PING_TARGET | grep Lost | awk '{print $12}') if [ $gwReachesNet -le 2 ]; then ## default GW reaches net, lets switch to it echo "=========="$datetimestamp"==========" echo "Default gateway is reachable again." wall "Default gateway is reachable again. Your connection will be lost in 30 seconds" sleep 30s ip route replace default via ${GW_IP[0]} dev eth0 echo "----------"$datetimestamp"----------" exit 0 fi fi else ## target unreachable echo "=========="$datetimestamp"==========" echo "The current gateway is "$gateway" and does not work" ## do the nping for all GWs for i in {0..2} do result[i]=$(nping --icmp --icmp-type echo --source-ip ${SRC_IP[i]} --dest-mac ${GW_MAC[i]} --delay 500ms $PING_TARGET | grep Lost | awk '{print $12}') echo ${GW_NAME[i]}": "${result[i]} done ######### ## set GW to the highes working GW ######### for i in {0..2} do if [ ${result[i]} -le 2 ]; then ## change GW to new route ip route replace default via ${GW_IP[i]} dev eth0 ## update dynDNS entry here echo "Gateway switched from "$gateway" to "${GW_IP[i]}" ("${GW_NAME[i]}")." break fi done echo "----------"$datetimestamp"----------" fi