Difference between revisions of "Failover iproute2"

From MS Computech
Jump to: navigation, search
(Created page with ''''iproute2 failover script''' <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 intermediate revisions by the same user not shown)
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>
 +
 +
----
 +
 +
'''Failover'''
 
<source lang=bash>
 
<source lang=bash>
 
#!/bin/sh
 
#!/bin/sh
Line 34: Line 62:
 
         if [ "$RET" -ne "$PACKETS" ]; then
 
         if [ "$RET" -ne "$PACKETS" ]; then
 
                 if [ "$USINGWAN" = "1" ]; then
 
                 if [ "$USINGWAN" = "1" ]; then
                         /jffs/etc/config/wan2.up
+
                         /app/wan2.up
 
                         USINGWAN=2
 
                         USINGWAN=2
 
                         echo "Changed active WAN port to 2!"
 
                         echo "Changed active WAN port to 2!"
 
                 else
 
                 else
                         /jffs/etc/config/wan1.up
+
                         /app/wan1.up
 
                         USINGWAN=1
 
                         USINGWAN=1
 
                         echo "Changed active WAN port to 2!"
 
                         echo "Changed active WAN port to 2!"
Line 44: Line 72:
 
         fi  
 
         fi  
 
done;
 
done;
 +
 +
 +
----
 +
  
 
</source>
 
</source>
 
[http://www.dd-wrt.com/wiki/index.php/Dual_WAN_with_failover Source]
 
[http://www.dd-wrt.com/wiki/index.php/Dual_WAN_with_failover Source]

Latest revision as of 19:35, 21 July 2009

iproute2 failover script


Wan1 up <source lang=bash>

  1. !/bin/sh
  2. WAN1

DEV=eth0 GATEWAY=192.168.101.1

ip route delete default

  1. ip route delete default

ip route add default via $GATEWAY dev $DEV </source>

Wan2 up <source lang=bash>

  1. !/bin/sh
  2. WAN2

DEV=eth1 GATEWAY=192.168.102.1

ip route delete default

  1. ip route delete default

ip route add default via $GATEWAY dev $DEV </source>


Failover <source lang=bash>

  1. !/bin/sh

INTERVAL=10 PACKETS=1 USINGWAN=0 WAN1=eth0 WAN2=eth1

  1. WAN1GW=`route -n | grep $WAN1 | grep '^0.0.0.0' | awk '{ print $2 }'`
  2. WAN2GW=`route -n | grep $WAN2 | grep '^0.0.0.0' | awk '{ print $2 }'`

WAN1GW=192.168.101.1 WAN2GW=192.168.101.2

  1. 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
                       /app/wan2.up
                       USINGWAN=2
                       echo "Changed active WAN port to 2!"
               else
                       /app/wan1.up
                       USINGWAN=1
                       echo "Changed active WAN port to 2!"
               fi
       fi 

done;




</source> Source