之前公司的系統由於網卡問題,經常出現掉包(掉包排除攻擊的 因素)或者突然掛掉,
嚴重影響服務的正常提供,因此想在出現此問題時自動解決。
思路:對網卡檢測掉包情況,或者用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環境估計問題不大,這裡就不測試鳥!