shell脚本-监控网络情况

前端之家收集整理的这篇文章主要介绍了shell脚本-监控网络情况前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/bin/bash
#-------------------------------------------------------------------------------
#FileName:networkchecking
#Revision:2.0
#Date:2017/09/14
#Author:XAD
#Email:1793360097@qq.com
#Website:http://onenice.blog.51cto.com/
#Description:checknetworkandsendmailtoXAD,wirteexecutinglog
#Notes:~
#-------------------------------------------------------------------------------
#Copyright:2017(c)XAD
#License:GPL

DOMAIN1=www.baidu.com
DOMAIN2=www.google.com
DATE=`date"+%F%H:%M:%S"`
HOST=`hostname`
IPADDRESS=`ifconfigeth0|grep"inetaddr:"|awk-F[:""]+'{print$4}'`

##打印成功日志##
log_info()
{
if[!-x/var/log/$0];then
mkdir/var/log/$0
fi
echo"$DATE$HOST$IPADDRESS$USERexecute$0>>$1">>/var/log/$0/success.log
}

##打印错误日志##
log_error()
{
if[!-x/var/log/$0];then
mkdir/var/log/$0
fi
echo"$DATE$HOST$IPADDRESS$USERexecute$0>>$1">>/var/log/$0/error.log
}

##检测网络是否通畅##
check_network()
{
ping-c2$DOMAIN1>/dev/null
pingstatus1=$?
echo-n$pingstatus1>/tmp/netwok-status
if[$pingstatus1-eq0];then
log_info"Networkto$DOMAIN1isok"
else
log_error"Networkto$DOMAIN1iserror"
fi

ping-c2$DOMAIN2>/dev/null
pingstatus2=$?
echo-n$pingstatus2>>/tmp/netwok-status
if[$pingstatus2-eq0];then
log_info"Networkto$DOMAIN2isok"
else
log_error"Networkto$DOMAIN2iserror"
fi
}

##发送警报邮件输出发送日志##
sendmail()
{
STATUS=`cat/tmp/netwok-status`
if["$STATUS"-eq11];then
echo"TIME:$DATEHOSTNAME:$HOSTIP:$IPADDRESSNetworkisnormal!"|mail-s"Pleasecheckyournetworknow!"1793360097@qq.com
if[$?-eq0];then
echo"$DATE$HOST$IPADDRESS$USER>>sendmailisok!">>/var/log/$0/sendmail.log
else
echo"$DATE$HOST$IPADDRESS$USER>>sendmailiserror!">>/var/log/$0/sendmail.log
fi
fi
}

check_network
sendmail
原文链接:https://www.f2er.com/bash/390782.html

猜你在找的Bash相关文章