#!/bin/sh
# Detect dead gateways
# NOTE:  the echo of the status returns 0
# 168.103.225.254 is next hop per traceroute:
while :; do
   ping -I eth2 -c1 168.103.225.254 >/dev/null 2>&1
   STAT=$?
   if [ ! $STAT = 0 ]; then
      echo -n "status $STAT at " >>/var/log/gw.94
      date >>/var/log/gw.94
   fi
   sleep 20
   ping -I eth1 -c1 68.69.31.169 >/dev/null 2>&1
   sleep 10
   ping -I eth1 -c1 206.72.89.155 >/dev/null 2>&1
#  STAT=$?
#  if [ ! $STAT = 0 ]; then
#     echo -n "status $STAT at " >>/var/log/gw.6
#     date >>/var/log/gw.6
#  fi
# yesican sends a y/n when it finds the gw up/down
# /var/tmp/gw can have 4 status values:
# y = GW is up and needs to have the default GW deleted
# n = GW is down and needs to have the default GW restored
# d = GW was down last time it was checked
# u = GW was up last time it was checked
   CHG=`cat /var/tmp/gw`
   if [ "$CHG" = "y" ]; then
# Make sure we're DOWN
      F=`cat /var/tmp/gw.6`
      if [ "$F" = "d" ]; then
         /root/scripts/deleteGW.Qwest
# Now that it is up, see if we can get anyone to use it
         /root/scripts/restoreGW.Adelphia
# flag as up
         echo -n u >/var/tmp/gw.6
         echo -n u >/var/tmp/gw
         echo -n "Up at " >>/var/log/gw.6
         date >>/var/log/gw.6
	 sleep 120
	 /root/scripts/deleteGW.Adelphia
      fi
   fi
   if [ "$CHG" = "n" ]; then
# Make sure we're UP
      F=`cat /var/tmp/gw.6`
      if [ "$F" = "u" ]; then
         /root/scripts/restoreGW.Qwest
# flag as down
         echo -n d >/var/tmp/gw.6
         echo -n d >/var/tmp/gw
         echo -n "Down  " >>/var/log/gw.6
         date >>/var/log/gw.6
      fi
   fi
   sleep 30
done

