linux網卡掉包或掛掉解決辦法

來源:互聯網
上載者:User


之前公司的系統由於網卡問題,經常出現掉包(掉包排除攻擊的 因素)或者突然掛掉,

嚴重影響服務的正常提供,因此想在出現此問題時自動解決。

思路:對網卡檢測掉包情況,或者用ping檢測,出現了,就重啟哈網卡 。瞧是很簡單吧!

代碼如下:

 代碼如下 複製代碼
#安裝協助: mkdir  /cyc
#/cyc/renetwork.log為記錄檔
 
#!/bin/bash
# 2013-2-17 @凹凸曼
# ping timeout  restart network
# lastmodtime 2013-3-19
pingNetwork(){
        local c=$1
        local pcount=$2
        local dst=192.168.0.143    #修改為你的互動機或者路由器ip
        if [[ ! $pcount =~ ^[0-9]+$ ]];then
                pcount=5
        fi
        loss=`ping -c $pcount  $dst|grep loss|awk '{print $6}'|cut -d% -f 1 `
        if [[ ! $loss =~ ^[0-9]+$ ]];then
                loss=100
        fi
 
        if [ $loss -gt 0 ]; then
                ifdown $c
                sleep 3
                ifup $c
                echo `date "+%Y%m%d %T"`": $pcount ping $loss% loss">>/cyc/renetwork.log
                sleep 30
        fi
 
}
 
#start dst eth
stopEth(){
        local c=$1
        local et=`service network status |tail -1|grep "$c"`
 
        if [ x"" == x"$et" ]; then
                ifup $c
                echo `date "+%Y%m%d %T"`":$c stop">>/cyc/renetwork.log
                sleep 30
        fi
}
#drop found
NetDrop(){
        local c=$1
        #$6 RX-DRP
        local redp=`netstat -i|grep "$c"|awk 'END{print $6}'`
        #$10 TX-DRP
        local txdp=`netstat -i|grep "$c"|awk 'END{print $10}'`
 
        if [ $redp -gt 0 ];then
                ifdown $c
                sleep 3
                ifup $c
                echo `date "+%Y%m%d %T"`":$c RX-DRP drop">>/cyc/renetwork.log
                sleep 30
        fi
 
        sleep 2
    if [ $txdp -gt 0 ];then
                ifdown $c
                sleep 3
                ifup $c
                echo `date "+%Y%m%d %T"`":$c TX-DRP drop">>/cyc/renetwork.log
                sleep 30
        fi
 
}
 
wtime=$2
eth=$1
if [[ ! $wtime =~ ^[0-9]+$ ]];then
  wtime=3
fi
if [ x"" == x"$eth" ];then
        echo "please enter the NIC name!"
        exit
fi
while [ 1 ]
        do
 
        stopEth $eth
        sleep $wtime
        NetDrop $eth
        sleep $wtime
        pingNetwork $eth 1
 
done
 


程式運行至目前的日誌結果如下:

 代碼如下 複製代碼
20130319 12:23:01: eth0 1 ping 100% loss
20130319 13:43:50: eth0 1 ping 100% loss
20130319 18:11:18: eth0 1 ping 100% loss
20130323 08:04:59: eth0 1 ping 100% loss
20130323 14:41:04: eth0 1 ping 100% loss
20130324 12:49:04: eth0 1 ping 100% loss
20130327 13:15:47: eth0 1 ping 100% loss
20130401 11:17:42: eth0 1 ping 100% loss
20130402 09:02:14: eth0 1 ping 100% loss
20130403 08:49:30: eth0 1 ping 100% loss
20130404 20:21:46: eth0 1 ping 100% loss
20130407 18:44:57: eth0 1 ping 100% loss
20130408 10:46:53: eth0 1 ping 100% loss
20130408 14:41:06: eth0 1 ping 100% loss
20130408 14:47:24: eth0 1 ping 100% loss
20130408 15:23:02:eth0 RX-DRP drop
20130415 08:30:09: eth0 1 ping 100% loss
20130415 11:16:16: eth0 1 ping 100% loss
20130417 08:05:41: eth0 1 ping 100% loss
20130419 08:04:19: eth0 1 ping 100% loss
20130419 10:40:51: eth0 1 ping 100% loss
20130419 11:33:14: eth0 1 ping 100% loss
20130419 19:03:03: eth0 1 ping 100% loss
20130421 16:10:55: eth0 1 ping 100% loss
20130422 07:47:34: eth0 1 ping 100% loss
20130423 07:15:07: eth0 1 ping 100% loss
 


呵呵,自動解決了網卡掉包或掛掉的煩惱!您是不是很爽!
忘記了說鳥,此程式運行環境Red Hat Enterprise Linux Server release 5.4或者Centos6.3
其他linux環境估計問題不大,這裡就不測試鳥!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.