Created
May 26, 2019 15:29
-
-
Save rwasef1830/2e5b8e8021cbef22c1c0d8e41f3c1b1c to your computer and use it in GitHub Desktop.
DD-WRT 4G WAN Failover Script
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/sh | |
INTERVAL=10 | |
PACKETS=1 | |
USINGWAN=0 | |
WAN1=ppp0 | |
WAN2=eth3 | |
# We'll run this in the intervals given above | |
while sleep $INTERVAL | |
do | |
WAN1GW=`nvram get wan_gateway` | |
WAN2GW=192.168.8.1 | |
# Try to figure out the current route | |
TARGET=`ip route | awk '/default via/ {print $3}'` | |
# Set the variable, so let the script now which connection is it dealing with | |
if [ "$WAN1GW" = "$TARGET" ]; then | |
USINGWAN=1; | |
else if [ "$WAN2GW" = "$TARGET" ]; then | |
USINGWAN=2; | |
fi; | |
fi | |
# We'll ping as many times the $PACKETS variable tells, and test if we have connection: | |
RET=`ping -c $PACKETS $TARGET 2>/dev/null | awk '/packets received/ {print $4}'` | |
echo "Result of pinging $TARGET: $RET packet(s) / $PACKETS" | |
# If we don't have connection, change the active WAN port (If there is any loss with multiple packets, it should change as well) | |
if [ "${RET:-0}" -ne "$PACKETS" ]; then | |
if [ "$USINGWAN" = "1" ]; then | |
ip route delete default; | |
ip route add default via $WAN2GW dev $WAN2 | |
USINGWAN=2 | |
echo "Changed active WAN port to 2!" | |
logger "Changed active WAN port to 2!" | |
else | |
ip route delete default; | |
ip route add default via $WAN1GW dev $WAN1 | |
USINGWAN=1 | |
echo "Changed active WAN port to 1!" | |
logger "Changed active WAN port to 1!" | |
fi | |
fi | |
if [ "$USINGWAN" = "2" ]; then | |
WAN1STAT=`ping -c $PACKETS $WAN1GW 2>/dev/null | awk '/packets received/ {print $4}'` | |
echo "Result of pinging $WAN1GW: $WAN1STAT packet(s) / $PACKETS" | |
if [ "${WAN1STAT:-0}" = "$PACKETS" ]; then | |
ip route delete default; | |
ip route add default via $WAN1GW dev $WAN1 | |
USINGWAN=1 | |
echo "Restored active WAN port to 1!" | |
logger "Restored active WAN port to 1!" | |
fi | |
fi | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment