Dead gateway detection on linux

If gateway responds to ICMP requests, you may use ping:

ping -qc 4 $GATE1 > /dev/null

if [ "$?" -ne 0 ];
ping -qc 4 GATE2 > /dev/null
if [ "$?" -ne 0 ];
 ping -qc 4 $GATE3 > /dev/null
 if [ "$?" -ne 0 ];
  ip route del default;ip route add default via $GATE3
 else
  echo "Three gateway down!"
 fi
else
 ip route del default;ip route add default via $GATE2
fi

If gateway doesn't respond to ICMP requests, change "ping -qc 4" to "ping -qc 4 -I IP_OF_INTERFACE_TO_GATE_N 8.8.8.8"


Why not run rip or another routing protocol? That's exactly what they're designed for. Quagga runs on Linux (amongst others) and implements a number of these protocols very well.

In my own systems I use ucarp to manage a virtual IP and then advertise the virtual address as the gateway to my clients via DHCP. Then the real gateways use rip to decide what to actually do with packets they receive.