腳本收集于恩山藐窄,親測可用于Openwrt系統(tǒng)寬帶掉線檢測與重?fù)埽?min即可執(zhí)行一次并生成my_pppoe.log 日志文件。
#!/bin/sh
#網(wǎng)絡(luò)檢測腳本 通過PING來判斷網(wǎng)絡(luò)互通狀態(tài)
#參數(shù)
#testing_ip=目標(biāo)IP地址 detection_times=檢測次數(shù) Interval_time=間隔時間(秒)
#狀態(tài)解釋state
#start=開始檢測 ok=網(wǎng)絡(luò)連通 no=網(wǎng)絡(luò)斷開重試
#restart wan=重連WAN restart wan ok=重連WAN完成
#restart network=重啟網(wǎng)絡(luò)進(jìn)程 restart network ok=重啟網(wǎng)絡(luò)進(jìn)程完成
#restart reboot=重啟路由 restart reboot ok=重啟路由完成
#腳本目錄下日志文件my_pppoe.log 最大512K超出自動清空
#BY:朱君綽綽 V1.4
#其他重啟命令
#ifup wan=重連WAN
#/etc/init.d/network restart=重啟網(wǎng)絡(luò)進(jìn)程
#reboot=重啟路由
#shell測試命令
#./my_pppoe.sh restart1
#echo $1
function Get_local_time(){
date_time=`date +"%Y-%m-%d %H:%M:%S"`;
}
function text_log_size(){
if [ -f $1 ]
then
filesize=`ls -l $1 | awk '{ print $5 }'`
maxsize=$((1024*512))
if [ $filesize -gt $maxsize ]
then
echo "$filesize > $maxsize"
echo "log cleared" >$1
else
echo "$filesize < $maxsize"
fi
fi
}
testing_ip=114.114.114.114
testing_ip2=202.108.22.5
detection_times=3
Interval_time=10
detection_count=0
WebError=0
text_log=my_pppoe.log
text_log_size $text_log
Get_local_time
echo $date_time -- my_pppoe -- state:start >>$text_log
while [[ $detection_count -lt $detection_times ]]
do
if /bin/ping -c 1 $testing_ip >/dev/null
then
Get_local_time
echo $date_time -- my_pppoe -- state:ok ping=$testing_ip>>$text_log
WebError=0
break
else
if /bin/ping -c 1 $testing_ip2 >/dev/null
then
Get_local_time
echo $date_time -- my_pppoe -- state:no ping=$testing_ip -->>$text_log
echo $date_time -- my_pppoe -- state:ok ping=$testing_ip2>>$text_log
WebError=0
break
else
detection_count=$((detection_count + 1))
Get_local_time
echo $date_time -- my_pppoe -- state:no ping=$testing_ip -->>$text_log
echo $date_time -- my_pppoe -- state:no ping=$testing_ip2 -->>$text_log
echo $date_time -- my_pppoe -- state:ping retry $detection_count-$detection_times>>$text_log
WebError=1
sleep $Interval_time
fi
fi
done
if [ $WebError = 1 ]
then
if [ $1 = restart1 ]
then
Get_local_time
echo $date_time -- my_pppoe -- state:restart wan >>$text_log
ifup wan
Get_local_time
echo $date_time -- my_pppoe -- state:restart wan ok >>$text_log
fi
if [ $1 = restart2 ]
then
Get_local_time
echo $date_time -- my_pppoe -- state:restart network >>$text_log
/etc/init.d/network restart
Get_local_time
echo $date_time -- my_pppoe -- state:restart network ok >>$text_log
fi
if [ $1 = restart3 ]
then
Get_local_time
echo $date_time -- my_pppoe -- state:restart reboot >>$text_log
Get_local_time
echo $date_time -- my_pppoe -- state:restart reboot ok >>$text_log
reboot
fi
fi
exit 0
#end