一聚教程网:一个值得你收藏的教程网站

热门教程

linux网卡掉包或挂掉解决办法

时间:2022-11-14 22:08:36 编辑:袖梨 来源:一聚教程网


之前公司的系统由于网卡问题,经常出现掉包(掉包排除攻击的 因素)或者突然挂掉,

严重影响服务的正常提供,因此想在出现此问题时自动解决。

思路:对网卡检测掉包情况,或者用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环境估计问题不大,这里就不测试鸟!

热门栏目