linux shell 判断主机三次存活之批量探测
在生产环境中,需要查看主机在线情况,之前都是采用执行一次ping命令的方式去操作,会丢失某些存活主机,故改进了脚本采用ping三次的方式进行操作。
ip_list="192.168.10.11 192.168.10.12 192.168.10.13 192.168.10.14 192.168.10.15 192.168.10.16 192.168.10.17 "
for ip in $ip_list
do
for count in {1..3}
do
ping -c1 -W1 $ip >/dev/null
if [ $? -eq 0 ];then
echo "$ip is ok"
break;
else
failcount[$count]=$ip
fi
if [ ${#failcount[*]} -eq 3 ];then
echo "${failcount[1]} is failure"
unset failcount[*]
fi
done
done
while read ip
do
{
for count in {1..3}
do
ping -c1 -W1 $ip >/dev/null
if [ $? -eq 0 ];then
echo "$ip is ok"
break;
else
failcount[$count]=$ip
fi
if [ ${#failcount[*]} -eq 3 ];then
echo "${failcount[1]} is failure"
unset failcount[*]
fi
done
}&
done <ip.txt
wait
|