Openwrt軟路由定時掉線檢測與重?fù)苣_本

腳本收集于恩山藐窄,親測可用于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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子叮盘,更是在濱河造成了極大的恐慌,老刑警劉巖霹俺,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柔吼,死亡現(xiàn)場離奇詭異,居然都是意外死亡丙唧,警方通過查閱死者的電腦和手機(jī)愈魏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來想际,“玉大人培漏,你說我怎么就攤上這事『荆” “怎么了牌柄?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長侧甫。 經(jīng)常有香客問我珊佣,道長,這世上最難降的妖魔是什么披粟? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任咒锻,我火速辦了婚禮,結(jié)果婚禮上僻爽,老公的妹妹穿的比我還像新娘。我一直安慰自己贾惦,他們只是感情好胸梆,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布敦捧。 她就那樣靜靜地躺著,像睡著了一般碰镜。 火紅的嫁衣襯著肌膚如雪兢卵。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天绪颖,我揣著相機(jī)與錄音秽荤,去河邊找鬼。 笑死柠横,一個胖子當(dāng)著我的面吹牛窃款,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播牍氛,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼晨继,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了搬俊?” 一聲冷哼從身側(cè)響起紊扬,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎唉擂,沒想到半個月后餐屎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡玩祟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年腹缩,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片卵凑。...
    茶點(diǎn)故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡庆聘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出勺卢,到底是詐尸還是另有隱情伙判,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布黑忱,位于F島的核電站宴抚,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏甫煞。R本人自食惡果不足惜菇曲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望抚吠。 院中可真熱鬧常潮,春花似錦、人聲如沸楷力。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至岔留,卻和暖如春夏哭,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背献联。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工竖配, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人里逆。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓进胯,卻偏偏與公主長得像,于是被迫代替她去往敵國和親运悲。 傳聞我的和親對象是個殘疾皇子龄减,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容