1

I have two WRT routers each connected to their own wireless gateway (cell phone and hotspot across the street). The routers lans are bridged with WDS and on the same subnet, so clients can connect to either gateway (depending on their network settings). I am trying to implement a script so that Router 2's gateway is changed to point at Router 1 when Router 2's gateway can't reach the internet (when we turn off the hotspot on the phone or when the phone is not in the home). It needs to change back when Router 2's gateway is back up.

As documented here Dual Gateway with failover, I found the script below, but after implementing it into the startup scripts it's not changing my gateway when I disconnect the main gateway.

I have read and read about multiple gateways, and different static routes with different metrics, and when one is unreachable it defaults to the other route. If that is so and can be implemented on a WRT router in Repeater Bridge mode then is the type of script below even necessary, and would anyone point me in the right direction? Thanks.

INTERVAL=6 
PACKETS=2 
USINGWAN=0 
GW=br0 
WAN1GW=192.168.3.4 
WAN2GW=192.168.3.1 
DNS1=202.88.149.25 
CH=1 

while sleep $INTERVAL 
do 
TARGET=`ip route | awk '/default via/ {print $3}'` 
if [ "$WAN1GW" = "$TARGET" ]; then 
USINGWAN=1; 
else if [ "$WAN2GW" = "$TARGET" ]; then 
USINGWAN=2; 
fi; 
fi 
RET=`ping -c $PACKETS $DNS1 2>/dev/null | awk '/packets received/ {print $4}'` 
if [ "$RET" -lt "$CH" ]; then 
if [ "$USINGWAN" = "1" ]; then 
ip route delete default via $WAN1GW dev $GW 
ip route add default via $WAN2GW dev $GW 
sleep 2 
USINGWAN=2 
echo "Changed active WAN port to 2!" 
fi 
fi 
if [ "$USINGWAN" = "2" ]; then 
WAN1STAT=`ping -c $PACKETS $DNS1 2>/dev/null | awk '/packets received/ {print $4}'` 
if [ "$WAN1STAT" = "$PACKETS" ]; then 
ip route delete default via $WAN2GW dev $GW 
ip route add default via $WAN1GW dev $GW 
sleep 2 
USINGWAN=1 
echo "Changed active WAN port to 1!" 
fi 
fi 
done; 

Below are the creator/users comments:
Used the code above with ping to only one ip, added a static route in gateway 192.168.3.1 for ping ip to be always routed through 192.168.3.4 as a result of which in case of ping failure gateway changed to secondary gateway and once ping was back live it switched to primary... Also used -lt variable to make sure that gateway changed on at least 2 ping failures and not 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.