首页 > 代码库 > 【shell】通过shell编写ping包及arp的监控并发送短信

【shell】通过shell编写ping包及arp的监控并发送短信

  1. #!/bin/bash
  2. NOW="`date +%Y%m%d-%H:%M:%S`"
  3. PHONES=15134567893
  4. IP=10.100.8.78
  5. GATEWAY=10.100.30.1
  6. #ping失误的短信内容
  7. content1="host is failed"
  8. #arp主机网关失误的短信内容
  9. content2="GW is failed"
  10. #ping日志路径
  11. LogFile="/tmp/ping`date "+%Y%m%d-%H-%M-%S"`.log"
  12. #ping对方主机100个包并将结果输出到日志
  13. ping -c100 $IP &> $LogFile
  14. #用if,else语句判断是否ping通,
  15. if [ "$?" == "0" ]; 
  16.     then
  17.         echo "$NOW $IP is ok">> $LogFile
  18. else
  19.         echo "$NOW $IP is down">> $LogFile &/usr/bin/curl -d "phones=$PHONES&content= $NOW $IP $content" "content-type: application/x-www-form-urlencoded;charset=UTF-8" "http://短信网关:8080"
  20. fi
  21. #分隔符
  22. echo "================================" >> $LogFile
  23. #arp自己网关
  24. arp $GATEWAY &>>$LogFile
  25. num=$(cat $LogFile |grep eth0|wc -l)
  26.     if [ "$num" == "0" ]; 
  27.         then
  28.           echo "$NOW $GATEWAY is down">> $LogFile &/usr/bin/curl -d "phones=$PHONES&content= $NOW $GATEWAY $content1" "content-type: application/x-www-form-urlencoded;charset=UTF-8" "http://短信网关:8080"
  29. fi

【shell】通过shell编写ping包及arp的监控并发送短信