Hello @ all,

this is a little script, to check your WAN connection every 5 minutes.
Notice: Cron must be installed, see: Crobtab Tutorial

explanation:
  • The script checks several times (parameter "ROUNDS_PER_SESSION") the connection via ping to the given host (parameter "CHECK_HOST").
  • Between the ping tests are X seconds (parameter "TIME_BETWEEN_ROUNDS").
  • Is the host not reachable several times ("ROUNDS_PER_SESSION") the script makes a reboot. Usefully if your WAN connection is overloaded for a short time.
  • Only X reboots allowed (parameter "ALLOWED_REBOOTS"), usefully if your WAN connection goes down for several hours.
  • If you like, the script ignore the user disconnect (parameter "IGNORE_USER_DISCONNECT"), on page: Status @Log --> Status --> Disconnect.
  • If you would like infos, when the WAN connection fails, you can turn on the debug (parameter "DEBUG"). If the connection fails, you get an entry on the debug file (parameter "DEBUG_LOG_FILE"). You get also an Entry if the WAN connection comes up after a failure.


And here are the script:
Code:
#!/bin/sh
##############################################
# Content of /opt/etc/cron.5mins/CheckWan.sh #
##############################################

##############################################
# Settings:                                  #
##############################################
# check Host (IP or Alias):
CHECK_HOST=www.google.de
# checkrounds per router session (between router reboots):
ROUNDS_PER_SESSION=6
# time between checkrounds (in seconds):
TIME_BETWEEN_ROUNDS=10
# allowed reboots
ALLOWED_REBOOTS=3
# ignore user disconnect (0/1):
IGNORE_USER_DISCONNECT=0
# debug off/on (0/1):
DEBUG=1
# debug log file:
DEBUG_LOG_FILE=/tmp/harddisk/CheckWan.log

# preparation:
i=1
if [ "`nvram get WAN_CHECK_FAILURE`" == "" ]; then nvram set WAN_CHECK_FAILURE=0; fi

# main script
if [ $IGNORE_USER_DISCONNECT == 0 ]; then
  if [ "`nvram get wan_reason_t`" == "User request" ]; then
    # Exiting script because user request!
    exit
  fi
fi

while [ $i -le $ROUNDS_PER_SESSION ]; do
  CHECK_WAN=`ping -c 1 $CHECK_HOST | grep packet | awk '{print $4}'` # Returns 1 if the WAN connection is ok.
  if [ "$CHECK_WAN" == "1" ]; then
    # WAN connection ok.
    if [ $(nvram get WAN_CHECK_FAILURE) != 0 ]; then
      # set WAN_CHECK_FAILURE to 0 if WAN_CHECK_FAILURE > 0
      if [ $DEBUG == 1 ]; then echo WAN connection establish on: $(date) >> $DEBUG_LOG_FILE; fi
      nvram set WAN_CHECK_FAILURE=0
      flashfs save
      flashfs commit
      flashfs enable
    fi
    break
  else
    # WAN connection not ok.
    if [ $i == $ROUNDS_PER_SESSION ]; then
      if [ $(nvram get WAN_CHECK_FAILURE) != $ALLOWED_REBOOTS ]; then
        # reboot
        if [ $DEBUG == 1 ]; then echo WAN connection failure [`expr $(nvram get WAN_CHECK_FAILURE) + 1`]: $(nvram get wan_reason_t) on $(date) >> $DEBUG_LOG_FILE; fi
        nvram set WAN_CHECK_FAILURE=`expr $(nvram get WAN_CHECK_FAILURE) + 1`
        flashfs save
        flashfs commit
        flashfs enable
        reboot
      fi
    else
      if [ $(nvram get WAN_CHECK_FAILURE) == $ALLOWED_REBOOTS ]; then
        # break script if ALLOWED_REBOOTS is reached
        break
      fi
    fi
    sleep $TIME_BETWEEN_ROUNDS
  fi
  i=`expr $i + 1`
done
Don't forget to make the script executable:
Code:
chmod +x /opt/etc/cron.5mins/CheckWan.sh
Here is a example debug log file:
Code:
WAN connection failure [1]: Peer not responding on Thu May 3 10:00:53 MESZ 2007
WAN connection failure [2]: Peer not responding on Thu Jan 1 01:05:53 MEZ 1970
WAN connection failure [3]: Peer not responding on Thu Jan 1 01:05:53 MEZ 1970
WAN connection establish on: Thu May 3 10:30:01 MESZ 2007
The wrong timestamp on line 2 and 3 is an WL500g reason: The router saves not the date and time over a reboot.

I hope you find this script useful.

Ciao rj.2001