#!/bin/sh
# author: [email protected]
# script for monitoring internet connection
# uses following crontab (/var/spool/cron/crontabs/<routeradminname>):
# * * * * * /jffs/check_net_routerv
# restoring crontab with /jffs/cp_netchecker (called after boot in result of the USB "hack", nvram rows below):
# contets of /jffs/cp_netchecker:
# cp /jffs/<rooteradminname> /var/spool/cron/crontabs/
#
# SO, copy this file as /jffs/check_net_routerv AND run the following commands:
# echo "* * * * * /jffs/check_net_routerv" > /jffs/<rooteradminname>
# cp /jffs/<rooteradminname> /var/spool/cron/crontabs/
# service restart_crond
# echo "cp /jffs/<routeradminname> /var/spool/cron/crontabs/" > /jffs/cp_netchecker
# chmod 755 /jffs/check_net_routerv /jffs/cp_netchecker
# nvram set script_usbmount="/jffs/cp_netchecker"
# nvram commit
THRESHOLD="3" #how much we tolerate
PRB_FILE="/jffs/asus_nw_problems"
ping -c2 8.8.8.8 > /dev/null
# if internet connection down
if [[ "$?" -ne 0 ]];then
echo "We're screwed!" >> /tmp/netwatch
MINUTES="$(wc -l /tmp/netwatch | awk '{print $1}')"
DATE=$(date +%Y%m%d_%H%M)
#try to renew the IP from ISP first
if [[ "$MINUTES" == "1" ]];then
echo -e "$DATE:\tInternet connection was down, trying renewing the IP." >> "$PRB_FILE"
/sbin/udhcpc -q -i usb0
#restart modem after 2 minutes of outage
elif [[ "$MINUTES" == "2" ]];then
echo -e "$DATE:\tInternet connection was down for 2 minutes restarting modem." >> "$PRB_FILE"
/usr/sbin/modem_enable.sh
# if down for THRESHOLD minutes (cron runs this every minute), reboot
elif [[ "$MINUTES" -ge "$THRESHOLD" ]];then
echo -e "$DATE:\tNetwork was down for $MINUTES minutes, rebooting router." >> "$PRB_FILE"
/sbin/reboot
fi
# if network ok, remove monitoring file
else
echo -e "\nInternet connection OK.\n"
if [ -s /tmp/netwatch ];then
rm /tmp/netwatch
fi
fi