欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

ping发现掉包报警的shell代码

程序员文章站 2022-06-02 13:10:30
复制代码 代码如下: #!/bin/bash ping=`which ping` date=`date +%y%m%d%h%m` tail=`which tail` log...
复制代码 代码如下:

#!/bin/bash
ping=`which ping`
date=`date +%y%m%d%h%m`
tail=`which tail`
log=./ping$date.log
hosts="selboo.com.cn 221.130.191.97"
count=200
for myhost in $hosts
do
count=$(ping -c $count $myhost | grep ‘loss' | awk -f',' '{ print $3 }' | awk -f “%” ‘{ print $1 }')
if [ $count -ge 10 ]; then
/bin/echo selboo | mutt -s "$myhost pingispacketloss>10% $date" root@selboo.com.cn
fi
done
exit 0

复制上以上内容为ping.sh加入crontab中让其10分钟跑一次,其中发邮件程序为mutt

以下是补充:

linux 的和solaris的居然不一样!
复制代码 代码如下:

#!/bin/sh
pingconf='/opt/monitor/newip'
#file where the log will be stored
logdir='/opt/monitor/newlog'
#define the interval time
interval='10'
#define the alarm process
########################################################
proc_main()
{
while read ipaddress
do
#ingore any hash signs
case $ipaddress in
#*);;
*)
ping -w 20 $ipaddress -c 5 >errtmp$$
# ping $ipaddress -c 10 >errtmp$$
if [ $? = 0 ] ; then
rm errtmp$$
else
rm errtmp$$
errtime=`date +%y/%m/%d/%h:%m:%s`
# echo "$errtime"": ""$ipaddress"" cann't be connected">>$logdir
echo "$errtime"":""$ipaddress"" cann't be connected
">>$logdir
sleep 1
cp /opt/monitor/newlog /opt/ov/www/htdocs/pinglog/new.html
sleep 1
# mail -s "$ipaddress"cantreach -c 我的email </dev/null
play -v 50 /opt/monitor/123.au
fi
esac
done <$pingconf
}
##########################################################
# execute the proc_main function every the specified time interval
while [ "1" -eq "1" ]
do

# execute the proc_main function
proc_main
# suspend execution for interval seconds
sleep $interval
done