Prior to the company's system due to the problem of network cards, often a switch (switch excluding the attack factors) or suddenly hung up,
Seriously affects the normal availability of the service, so you want to resolve it automatically when this problem occurs.
Train of thought: Check the switch of the NIC, or use ping to detect, appear, restart the network card. Look, it's very simple!
The code is as follows:
The code is as follows |
Copy Code |
#安装帮助: MKDIR/CYC #/cyc/renetwork.log as log file
#!/bin/bash # 2013-2-17 @ Bump Mann # 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 $} ' |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 $} ' #$10 TX-DRP Local txdp= ' netstat-i|grep ' $c ' |awk ' end{print $} '
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] Todo
Stopeth $eth Sleep $wtime Netdrop $eth Sleep $wtime Pingnetwork $eth 1
Done |
The program runs to the current log results as follows:
The code is as follows |
Copy Code |
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
|
Oh, automatically solve the network card switch or hang off the trouble! You are not very cool!
Forgot to say bird that this program running environment Red Hat Enterprise Linux Server release 5.4 or Centos6.3
Other Linux environment estimates are not the problem, here is not the test bird!