#!/bin/bash # 加入计划任务 2分钟执行一次,1分钟太短 DT=`date +%Y-%m-%d` TIME=`date +%H:%M:%S` LOG="/tmp/check-eth0-$DT.log" IP="10.0.0.3" # /var/log/message MESSAGE="genet eth0: Link is Up" # 网卡启动时间 LINKUP=`grep "$MESSAGE" /var/log/messages |tail -n 1 |awk '{print $1,$2,$3}'` LINKUP_TIMESTAMP=`date -d "$LINKUP" +%s` NOW_TIMESTAMP=`date +%s` ((DIFFER=$NOW_TIMESTAMP-$LINKUP_TIMESTAMP)) if [ $DIFFER -gt 300 ];then echo "$TIME eth0 up more than 5 minutes. ignore" |tee -a $LOG else for id in `seq 1 5`;do nmap -sPn $IP done |grep "0 hosts up" if [ $? -eq 0 ];then ifconfig eth0 down ifconfig eth0 up echo "$TIME found problem. restart eth0..." |tee -a $LOG else echo "$TIME link ok" |tee -a $LOG fi fi