1.简单检测网络当出现网络异常时,则重启网卡
#!/bin/bash ping -c 2 192.168.1.1 >/dev/null if [ "$?" -ne "0" ] then /sbin/ifdown eth0; /sbin/ifup eth0 else echo "network is staut" >> /root/newlog fi
2.复杂型检测主机网络当出现异常时,则重启网卡
#!/bin/bash DIR=/etc/sysconfig/network-scripts/ ETH=`cat $DIR/ifcfg-* |grep "DEVICE"|grep -v "lo"|cut -b 9-12` Route=`route -n |awk '{print $2} |grep -vE "IP|Gateway|0.0.0.0"` Date=`date +"%Y-%m-%d %A %H:%M:%S"` /bin/ping -c 2 $Route > /dev/null if [ "$?" -ne "0" ] then echo "Network Restarting" echo $Date Network Restarting >> /var/log/Checklog /sbin/ifdown $ETH; /sbin/ifup $ETH >> /var/log/Checklog else echo "$Date Checking Network is ok" >> /var/log/Checklog echo $Date Checking HOST $ETH IS STATUS >> /var/log/Checklog fi
3.为了更好的体现此脚本的实用价值,建议添加至计划任务,每五分钟执行检测如下
~#crontab -e */5 * * * * /root/CheckNet.sh ~#service crond restart