#!/bin/bash# -------------------------------------------------------------------------------# filename: networkchecking# revision: 2.0# date: 2017/09/14# author: xad# Email: [email protected]# Website: http://onenice.blog.51cto.com/# description: check network and sendmail to XAD , wirte executing log# Notes: ~# -------------------------------------------------------------------------------# copyright: 2017 (c) xad# license: gpldomain1=www.baidu.comdomain2= Www.google.comDATE= ' date "+%f %h:%m:%s" ' host= ' hostname ' ipaddress= ' ifconfig eth0| grep "INET ADDR:" | awk -f[: " "]+ ' {print $4} ' # #打印成功日志 # #log_info () { if [ ! -x /var/log/$0 ];then mkdir /var/log/$0 fi echo "$DATE $HOST $ ipaddress $USER execute $0 >> $1 " >> /var/log/$0/ success.log}# #打印错误日志 # #log_error () { if [ ! -x /var/log/$0 ]; then mkdir /var/log/$0 fi echo $DATE $HOST $IPADDRESS $USER execute $0 >> $1 " >>/var/log/$0/error.log}# #检测网络是否通畅 # #check_network () { ping -c 2 $DOMAIN 1 > /dev/null pingstatus1=$? echo -n&nBSP; $pingstatus 1 > /tmp/netwok-status if [ $pingstatus 1 -eq 0 ];then log_info "network to $ Domain1 is ok " else log_error "network to $DOMAIN 1 is error" fi ping -c 2 $DOMAIN 2 > /dev/null pingstatus2=$? echo -n $pingstatus 2 >> /tmp/netwok-status if [ $pingstatus 2 -eq 0 ];then log_info "network to $DOMAIN 2 is ok" else log_error "network to $DOMAIN 2 is Error " fi}# #发Send alert message and output send log # #sendmail () { status= ' cat /tmp/netwok-status ' if [ "$STATUS" -eq 11 ]; then echo "Time: $DATE hostname: $HOST ip: $IPADDRESS Network is normal! " | mail -s "please check your network now!" [email protected] if [ $? -eq 0 ];then echo "$DATE $ host $IPADDRESS $USER >> sendmail is ok ! " >> /var/log/$0/sendmail.log else echo "$DATE $HOST $IPADDRESS $ user >> sendmail is error ! " >> /var/log/$0/sendmail.log fifi}check_networksendmail
This article is from the "Top-one" blog, make sure to keep this source http://onenice.blog.51cto.com/13201315/1965399
Shell Scripts-Monitor network conditions