监控文件(ips.txt)里定义的所有的机器的网络情况,如果网络出现异常(ping不通)则记录到日志中。支持ipv4和ipv6。
监控脚本的具体内容如下:
#!/bin/bash
##########################################################################
# Author: #
# gfsunny@163.com #
# Create: #
# 2014/11/28 #
# Function: #
# Judge network status #
# Notes: You only need to configure "Email" #
##########################################################################
Email="gfsunny@163.com,282750085@qq.com"
CurrentDate=`date +%Y%m%d\ %H:%M:%S\ %A`
NetworkReportTmp=`date +%Y%m%d`.tmp
function emailinfo() {
echo "" > $NetworkReportTmp
echo "[$CurrentDate network report]" >> $NetworkReportTmp
# echo "" >> $NetworkReportTmp
}
function sendmail() {
mail -s "$CurrentDate:[Report - Network Report]" "$Email" < $NetworkReportTmp
}
function isIPv4() {
for i in `cat ips.txt | grep "^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}$"`;
do
ping -c 1 $i | grep -q "ttl="
if [ $? != 0 ];then
echo "$i is Unreachable" >> $NetworkReportTmp
else
echo "$i is yes" 2>/dev/null
fi
done
}
function isIPv6() {
# Judge CDCD:910A:2222:5498:8475:1111:3900:2020
for i in `cat ips.txt | grep "^([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}$"`;
do
ping -c 1 $i | grep -q "ttl="
if [ $? != 0 ];then
echo "$i is Unreachable" >> $NetworkReportTmp
else
echo "$i is yes" 2>/dev/null
fi
done
# Judge ::
for i in `cat ips.txt | grep "^::$"`;
do
ping -c 1 $i | grep -q "ttl="
if [ $? != 0 ];then
echo "$i is Unreachable" >> $NetworkReportTmp
else
echo "$i is yes" 2>/dev/null
fi
done
# Judge F:F:F::1:1 F:F:F:F:F::1 F::F:F:F:F:1
for i in `cat ips.txt | grep "^(([0-9a-f]{1,4}:){1,6})((:[0-9a-f]{1,4}){1,6})$"`;
do
ping -c 1 $i | grep -q "ttl="
if [ $? != 0 ];then
echo "$i is Unreachable" >> $NetworkReportTmp
else
echo "$i is yes" 2>/dev/null
fi
done
# Judge F:F:10F::
for i in `cat ips.txt | grep "^([0-9a-f]{1,4}:){1,7}:$"`;
do
ping -c 1 $i | grep -q "ttl="
if [ $? != 0 ];then
echo "$i is Unreachable" >> $NetworkReportTmp
else
echo "$i is yes" 2>/dev/null
fi
done
# Judge ::F:F:10F
for i in `cat ips.txt | grep "^:(:[0-9a-f]{1,4}){1,7}$"`;
do
ping -c 1 $i | grep -q "ttl="
if [ $? != 0 ];then
echo "$i is Unreachable" >> $NetworkReportTmp
else
echo "$i is yes" 2>/dev/null
fi
done
# Judge F:E:E:A:B:C:10.0.0.1
for i in `cat ips.txt | grep "^([0-9a-f]{1,4}:){6}(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$"`;
do
ping -c 1 $i | grep -q "ttl="
if [ $? != 0 ];then
echo "$i is Unreachable" >> $NetworkReportTmp
else
echo "$i is yes" 2>/dev/null
fi
done
# Judge F::10.0.0.1
for i in `cat ips.txt | grep "^([0-9a-f]{1,4}:){1,5}:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$"`;
do
ping -c 1 $i | grep -q "ttl="
if [ $? != 0 ];then
echo "$i is Unreachable" >> $NetworkReportTmp
else
echo "$i is yes" 2>/dev/null
fi
done
# Judge ::10.0.0.1
for i in `cat ips.txt | grep "^::(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$"`;
do
ping -c 1 $i | grep -q "ttl="
if [ $? != 0 ];then
echo "$i is Unreachable" >> $NetworkReportTmp
else
echo "$i is yes" 2>/dev/null
fi
done
}
#main()
emailinfo
isIPv4
isIPv6
sendmail
rm -f $NetworkReportTmp
原文地址:http://gfsunny.blog.51cto.com/990565/1584092