linux shell 判断主机三次存活之批量探测
在生产环境中,需要查看主机在线情况,之前都是采用执行一次ping命令的方式去操作,会丢失某些存活主机,故改进了脚本采用ping三次的方式进行操作。
#for 语句
#!/bin/bash
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/nullif [ $? -eq 0 ];thenecho "$ip is ok"break; else#echo "$ip ping faulure:$count"failcount[$count]=$ipfi if [ ${#failcount[*]} -eq 3 ];thenecho "${failcount[1]} is failure"unset failcount[*] fidone
done#while语句后台运行
#!/bin/bash
while read ip
#for ip in $ip_list
do
{for count in {1..3}doping -c1 -W1 $ip >/dev/nullif [ $? -eq 0 ];thenecho "$ip is ok"break;else#echo "$ip ping faulure:$count"failcount[$count]=$ipfiif [ ${#failcount[*]} -eq 3 ];thenecho "${failcount[1]} is failure"unset failcount[*]fidone
}&
done <ip.txt
wait
#后台运行可使脚本运行效率大大提高