联系我们:416326801#qq.com(#改成@)

img1

Linux检查网络并自动重启网卡

1056 Views 标签: Shell Linux ping

添加crontab定时任务,可以用作心跳检测.


[root@mail src]# cat check_network.sh 
#!/bin/bash
date="`date '+%Y-%m-%d %H:%M:%S'`"
ip="116.205.11.145"
nic="eth0"
lost_rate=`ping -c 8 -w 8 $ip | grep 'packet loss' \
                | awk -F'packet loss' '{ print $1 }' \
                | awk '{ print $NF }' | sed 's/%//g'`

if [ $lost_rate -eq 0 ]
then
echo "network_ok $date $ip $nic lose $lost_rate" >>/var/log/network_ok.log
elif [ $lost_rate -le 100 ]
then
echo "network_error $date $ip $nic lose $lost_rate" >>/var/log/network_error.log 
/sbin/service network restart
else
echo "network_error $date $ip $nic lose $lost_rate" >>/var/log/network_error.log
/sbin/service network restart
fi


如果觉得我的文章对您有用,打赏一下呗.

wxpay.png