网络状况自动检测脚本
分别 wget Google 和 Baidu,失败则尝试重启 shadowsocks 和 chinadns,仍失败则重启路由。
以下脚本需要先 opkg install wget,安装完整版的 wget 才可执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#!/bin/sh LOGTIME=$(date "+%Y-%m-%d %H:%M:%S") wget -4 -q -O /etc/ss/google.html --quiet --tries=3 --timeout=10 www.google.com.hk wget -4 -q -O /etc/ss/baidu.html --quiet --tries=3 --timeout=10 www.baidu.com g_size=$(ls -l /etc/ss/google.html | awk '{ print $5 }') b_size=$(ls -l /etc/ss/baidu.html | awk '{ print $5 }') error=0 if [ $g_size -gt 0 ] && [ $b_size -gt 0 ] ; then echo '['$LOGTIME'] No Problem.' >> /etc/ss/status.log rm /etc/ss/google.html rm /etc/ss/baidu.html exit 0 else error=1 fi if [ $error=1 ] ; then echo '['$LOGTIME'] Problem decteted. Restarting shadowsocks.' >> /etc/ss/status.log /etc/init.d/shadowsocks restart sleep 5 # /etc/init.d/chinadns restart sleep 5 LOGTIME=$(date "+%Y-%m-%d %H:%M:%S") wget -4 -q -O /etc/ss/google.html --quiet --tries=3 --timeout=10 www.google.com.hk wget -4 -q -O /etc/ss/baidu.html --quiet --tries=3 --timeout=10 www.baidu.com g_size=$(ls -l /etc/ss/google.html | awk '{ print $5 }') b_size=$(ls -l /etc/ss/google.html | awk '{ print $5 }') if [ $g_size -gt 0 ] && [ $b_size -gt 0 ] ; then echo '['$LOGTIME'] Problem fixed.' >> /etc/ss/status.log rm /etc/ss/google.html rm /etc/ss/baidu.html error=0 exit 0 else echo '['$LOGTIME'] Unable to fix the problem. Restarting router.' >> /etc/ss/status.log reboot -f fi fi |
最后设置 755 运行权限,定时执行即可。记得间隔时间千万不要太短。我手贱设置成了 1 分钟执行一次,结果它真的重启了,然而 1 分钟根本不够它完成联网,导致重启被无限循环,费了好大劲才旧救回来……
如果只检测百度可以使用下面的简化版:中间加了 pdnsd 等服务,不需要可以自己删除。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#!/bin/sh LOGTIME=$(date "+%Y-%m-%d %H:%M:%S") wget -4 -q -O /etc/ss/baidu.html --quiet --tries=3 --timeout=10 www.baidu.com b_size=$(ls -l /etc/ss/baidu.html | awk '{ print $5 }') if [ $b_size -gt 0 ] ; then echo '['$LOGTIME'] No Problem.' >> /etc/ss/status.log rm /etc/ss/baidu.html exit 0 else echo '['$LOGTIME'] Problem decteted. Restarting pdnsd, dnsmasq, dnscrypt.' >> /etc/ss/status.log /etc/init.d/pdnsd restart sleep 5 /etc/init.d/dnsmasq restart sleep 5 /etc/init.d/dnscrypt-proxy restart sleep 5 LOGTIME=$(date "+%Y-%m-%d %H:%M:%S") wget -4 -q -O /etc/ss/baidu.html --quiet --tries=3 --timeout=10 www.baidu.com b_size=$(ls -l /etc/ss/baidu.html | awk '{ print $5 }') if [ $g_size -gt 0 ] && [ $b_size -gt 0 ] ; then echo '['$LOGTIME'] Problem fixed.' >> /etc/ss/status.log rm /etc/ss/baidu.html exit 0 else echo '['$LOGTIME'] Unable to fix the problem. Restarting router.' >> /etc/ss/status.log #reboot -f fi fi |