Guest User

failover.sw by JMVS

a guest
May 2nd, 2012
698
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. ME="$(basename $0)" #Get name of the actual running script
  3. logger "$ME: Starting Failover Dual-Wan script..."
  4. RUNNING="$(ps | awk '/'"$ME"'/ {++x}; END {print x+0}')" #Check if there's another instance running
  5. if [ "$RUNNING" -gt 3 ]; then
  6.     echo "Another instance of \"$ME\" is running" #Another intance running, abort.
  7.     logger "$ME: Another instance of the Failover Dual-Wan script is already running, aborting..."
  8.     exit 1
  9. fi
  10. ### CONFIG START ################################################
  11. INT=25 #Interval to check connection in seconds (default: 25secs)
  12. PCK=1 #Number of packets to ping (default: 1 packet)
  13. ### CONFIG END ##################################################
  14. WAN1NM="$(nvram get wan_ifname)" #Get WAN 1 interface
  15. WAN2NM="$(nvram get wan2_ifname)" #Get WAN 2 interface
  16. WAN1GW="$(nvram get wan_gateway)" #Get WAN 1 gateway
  17. WAN2GW="$(nvram get wan2_gateway)" #Get WAN 2 gateway
  18. RWAN="$(ip route | awk '/default via/ {print $3}')" #Get active running gateway
  19. WARNBAK=0 #Variable set so we can output to the log only once per incident
  20. ### SCRIPT START ################################################
  21. gpio enable 2 #Turns off WPS LED (WRT310N v2)
  22. if [ "$WAN1GW" = "$RWAN" ]; then #Find out which WAN is active and running
  23.     AWANNM=$WAN1NM #Define active WAN 1 as connection
  24.     AWANGW=$WAN1GW
  25.     gpio disable 4 #Turn on blue LED (WRT310N v2)
  26.     gpio enable 2
  27. else
  28.     AWANNM=$WAN2NM #Define active WAN 2 as connection
  29.     AWANGW=$WAN2GW
  30.     gpio disable 4 #Turn on violet LED (WRT310N v2)
  31. fi
  32. logger "$ME: WAN1 - Interface: $WAN1NM / Gateway: $WAN1GW"
  33. logger "$ME: WAN2 - Interface: $WAN2NM / Gateway: $WAN2GW"
  34. logger "$ME: $AWANNM active via $AWANGW"
  35.  
  36. while [ 1 = 1 ] #Endless loop
  37. do
  38.     CHK="$(ping -c $PCK $AWANGW 2>/dev/null | awk '/packets received/ {print $4}')"
  39.     if [ "$CHK" -ne "$PCK" ]; then #Check if the connection is working
  40.         logger "$ME: $AWANNM interface not responding, switching..." #Connection down
  41.         ip route delete default #Delete actual routing
  42.         if [ "$AWANNM" = "$WAN1NM" ]; then
  43.             AWANNM=$WAN2NM
  44.             AWANGW=$WAN2GW
  45.             gpio disable 2 #Turn on violet LED
  46.         else
  47.             AWANNM=$WAN1NM
  48.             AWANGW=$WAN1GW
  49.             gpio enable 2 #Turn on blue LED
  50.         fi
  51.         ip route add default via $AWANGW dev $AWANNM #Establish new routing
  52.     fi
  53.     if [ "$AWANNM" = "$WAN2NM" ]; then
  54.         CHKWAN1="$(ping -c $PCK $WAN1GW 2>/dev/null | awk '/packets received/ {print $4}')"
  55.         if [ $WARNBAK = 0 ]; then
  56.             logger "$ME WARNING: Using backup interface"
  57.             $WARNBAK=1
  58.         fi
  59.         if [ "$CHKWAN1" = "$PCK" ]; then
  60.             logger "$ME: $WAN1NM interface working again, switching..."
  61.             $WARNBAK=0
  62.             ip route delete default #Delete actual routing
  63.             ip route add default via $WAN1GW dev $WAN1NM #Establish new routing
  64.             AWANNM=$WAN1NM #Define new active connection
  65.             gpio enable 2 #Turn on blue LED
  66.         fi
  67.     fi
  68. sleep $INT
  69. done;
  70. logger "$ME ERROR: Script terminated abnormaly."
RAW Paste Data