Difference between revisions of "Failover iproute2"
From MS Computech
Line 1: | Line 1: | ||
'''iproute2 failover script''' | '''iproute2 failover script''' | ||
+ | ---- | ||
+ | |||
+ | Wan1 up | ||
+ | <source lang=bash> | ||
+ | #!/bin/sh | ||
+ | # WAN1 | ||
+ | DEV=eth0 | ||
+ | GATEWAY=192.168.101.1 | ||
+ | |||
+ | ip route delete default | ||
+ | # ip route delete default | ||
+ | ip route add default via $GATEWAY dev $DEV | ||
+ | </source> | ||
+ | |||
+ | Wan2 up | ||
+ | <source lang=bash> | ||
+ | #!/bin/sh | ||
+ | # WAN2 | ||
+ | DEV=eth1 | ||
+ | GATEWAY=192.168.102.1 | ||
+ | |||
+ | ip route delete default | ||
+ | # ip route delete default | ||
+ | ip route add default via $GATEWAY dev $DEV | ||
+ | </source> | ||
---- | ---- |
Revision as of 19:32, 21 July 2009
iproute2 failover script
Wan1 up <source lang=bash>
- !/bin/sh
- WAN1
DEV=eth0 GATEWAY=192.168.101.1
ip route delete default
- ip route delete default
ip route add default via $GATEWAY dev $DEV </source>
Wan2 up <source lang=bash>
- !/bin/sh
- WAN2
DEV=eth1 GATEWAY=192.168.102.1
ip route delete default
- ip route delete default
ip route add default via $GATEWAY dev $DEV </source>
<source lang=bash>
- !/bin/sh
INTERVAL=10 PACKETS=1 USINGWAN=0 WAN1=eth0 WAN2=eth1
- WAN1GW=`route -n | grep $WAN1 | grep '^0.0.0.0' | awk '{ print $2 }'`
- WAN2GW=`route -n | grep $WAN2 | grep '^0.0.0.0' | awk '{ print $2 }'`
WAN1GW=192.168.101.1 WAN2GW=192.168.101.2
- We'll run this in the intervals given above
while sleep $INTERVAL do
# 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}'`
# If we don't have connection, change the active WAN port (If there is any loss with multiple packets, it should change either)
if [ "$RET" -ne "$PACKETS" ]; then if [ "$USINGWAN" = "1" ]; then /jffs/etc/config/wan2.up USINGWAN=2 echo "Changed active WAN port to 2!" else /jffs/etc/config/wan1.up USINGWAN=1 echo "Changed active WAN port to 2!" fi fi
done;
</source>
Source