注:原脚本存在大量重复报警的问题,改写后问题得以解决
实现一:借助临时文件
#!/bin/bash # PF=0 while true do touch /tmp/reppoe.txt PPP=`ifconfig -a | grep -o ppp` PING=` ping -c 4 www.baidu.com 2>&1 | grep ttl | wc -l` LINE=`cat /tmp/reppoe.txt | wc -l` if [ "$PPP" != 'ppp' ] || [ "$PING" -eq 0 ];then echo "`date` pppoe is down" >> /tmp/reppoe.txt if [ $LINE -gt 20 ]; then echo "pppoe is dowm " | mail -s "pppoe is down" mailaddr && PF=1 true > /tmp/reppoe.txt fi adsl-start; PING2=` ping -c 4 www.baidu.com 2>&1 | grep ttl | wc -l` if [ $PING2 != 0 -a $PF != 0 ];then IP=`LC_ALL=C ifconfig | grep -P -A2 "ppp" | grep 'inet addr' | cut -d: -f2 | awk '{ print $1}' ` echo "pppoe is up and ip is $IP" | mail -s "pppoe is up" mailaddr && PF=0 fi fi sleep 10 done
实现二:
#!/bin/bash mfcount=0 while true do PPP=`ifconfig -a | grep -o ppp` PING=` ping -c 4 www.baidu.com 2>&1 | grep ttl | wc -l` [ "$PPP" != 'PPP' ] || [ "$PING" -eq 0 ] status=$? if [ $status != 1 ] ;then mccount=`expr $mfcount + 1` [ $mfcount -lt 1 -o $mccount -gt 3 ] && echo "pppoe is down" | mail -s "pppoe is down" mailaddr && mfcount=`expr $mfcount + 1` ppstatus=1 while [ $ppstatus != 0 -o $PING = 0 ] do adsl-start; ppstatus=$? PING=` ping -c 4 www.baidu.com 2>&1 | grep ttl | wc -l` if [ $PING != 0 ] ;then echo ok && echo "pppoe is up and ip is $IP" | mail -s "pppoe is up" mailaddr && mfcount=0 fi sleep 3 done IP=`LC_ALL=C ifconfig | grep -P -A2 "eth0" | grep 'inet addr' | cut -d: -f2 | awk '{ print $1}' ` fi sleep 10 done
转载于:https://blog.51cto.com/sanyu/1252223