脚本检测路由网络状态

Create Directory
mkdir -p /root/home watchdoglog
Script Contents
vi /root/home/watchdog.sh
#!/bin/sh
#Detecting Network Connections
DATE=$(date "+%Y-%m-%d %H:%M:%S")
#echo $DATE >>/root/watchdoglog/my_watchdog.log
ping -c 1 114.114.114.114 > /dev/null 2>&1
if [ $? -eq 0 ];then
        if [ -f "/root/watchdoglog/my_watchdog.log" ];then
                while read LINE
                do
                        wget -s --spider 'http://homeeyes.cn/api/watchdog?rebot=rebot473&time='$(date -d "$LINE" +%s)
                        sleep 5
                done  < /root/watchdoglog/my_watchdog.log
                `find /root/watchdoglog/ -name "*.log" | xargs rm`
        else
                echo --- NO DIRECTIORY ---
        fi
else
        if [ -f "/root/watchdoglog/my_watchdog.log" ];then
                ROWNUMBER=$(awk 'END{print NR}' /root/watchdoglog/my_watchdog.log)
                if [ $ROWNUMBER -gt 5 ];then
                        LASTROW=$(tail /root/watchdoglog/my_watchdog.log -n 1)
                        PREROW=$(tail -2 /root/watchdoglog/my_watchdog.log|head -n 1)
                        TPREROW=$(tail -3 /root/watchdoglog/my_watchdog.log|head -n 1)
                        STARTIME=$(date +%s -d "$LASTROW")
                        ENDTIME=$(date +%s -d "$PREROW")
                        THREETIME=$(date +%s -d "$TPREROW")
                        DIFFERSECOND=$(($STARTIME-$ENDTIME))
                        SDIFFERSECOND=$(($ENDTIME-$THREETIME))
                        if [[ $DIFFERSECOND -eq 180 && $SDIFFERSECOND -eq 180 ]];then
                                echo --- NO INFO ---
                        else
                                echo $DATE >>/root/watchdoglog/my_watchdog.log
                                reboot
                        fi
                else
                        echo $DATE >>/root/watchdoglog/my_watchdog.log
                        reboot
                fi
        else
                echo $DATE >>/root/watchdoglog/my_watchdog.log
                reboot
        fi
fi
Create timed tasks
/etc/init.d/cron enable
/etc/init.d/cron start
crontab -e
*/3 * * * * sh /root/home/watchdog.sh

你可能感兴趣的:(脚本检测路由网络状态)