-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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