Skip to content

Instantly share code, notes, and snippets.

@rturk
Forked from Apsu/failover.sh
Last active November 29, 2023 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rturk/78debbce06de597302a420e1416c7ada to your computer and use it in GitHub Desktop.
Save rturk/78debbce06de597302a420e1416c7ada to your computer and use it in GitHub Desktop.
An example failover script for dual WAN, using a ping healthcheck and managing default routes appropriately
#!/bin/bash
# Set defaults if not provided by environment
CHECK_DELAY=${CHECK_DELAY:-5}
CHECK_IP=${CHECK_IP:-1.1.1.1}
PRIMARY_IF=${PRIMARY_IF:-eno8303}
PRIMARY_GW=${PRIMARY_GW:-192.168.15.1}
# Cycle healthcheck continuously with specified delay
while sleep "$CHECK_DELAY"
do
# If healthcheck succeeds from primary interface
if ping -I "$PRIMARY_IF" -c1 "$CHECK_IP" &>/dev/null
then
# Set Primary with more priority (ie. lower metric)
ip route add default via "$PRIMARY_GW" dev "$PRIMARY_IF" metric 100
else
# Set Primary with less priority (ie. higher metric)
ip route add default via "$PRIMARY_GW" dev "$PRIMARY_IF" metric 200
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment