一、ping三次,每次发送一个包,如果不设置会一直ping下去,3次都失败告警,避免网络延迟造成的误差。
二、编写shell脚本
#!/bin/bash
for ((i=1;i<4;i++))
do
#测试连通性
if ping -c1 $1 &> /dev/null
then
export ping_count"$i"=1
else
export ping_count"$i"=0
fi
#时间间隔
sleep 0.5
done
#3次ping失败报警
if [ $ping_count1 -eq $ping_count2 ] && [ $ping_count2 -eq $ping_count3 ] &&
[ $ping_count1 -eq 0 ]
then
echo "$1 is down"
else
echo "$1 is up"
fi
unset ping_count1
unset ping_count3
unset ping_count3
三、基于文件读取ip地址
#!/bin/bash
IP_LIST=`cat ip.lst`
for IP in $IP_LIST; do
FAIL_COUNT=0
for ((i=1;i<=3;i++)); do
if ping -c 1 $IP >/dev/null; then
echo "$IP Ping is successful."
break
else
# echo "$IP Ping is failure $i"
let FAIL_COUNT++
fi
done
if [ $FAIL_COUNT -eq 3 ]; then
echo "$IP Ping is failure!" >> fail2.txt
fi
done