自动脚本现在在机器上运行shutdown -r,在30秒延迟后,使用ping来确定机器何时可用.我最近将操作系统从Centos 5切换到了Oracle
Linux 6,发现ping的行为发生了变化.
我使用ping同时具有计数(-c10),截止时间(-w360)和延迟(-W1),这应该等待最多五分钟才能从机器中成功回复十次.
我观察自己的机器在30秒后生成目标主机无法访问的消息,导致ping在3个错误后退出,即.远在我期望的截止日期之前.例如.示例在~37秒后退出:
[cs@bst1 ~]# time ping -c10 -w360 -W1 hostother; echo $? PING hostother (10.210.51.155) 56(84) bytes of data. From bst1 (10.210.51.139) icmp_seq=36 Destination Host Unreachable From bst1 (10.210.51.139) icmp_seq=37 Destination Host Unreachable From bst1 (10.210.51.139) icmp_seq=38 Destination Host Unreachable --- hostother ping statistics --- 38 packets transmitted,0 received,+3 errors,100% packet loss,time 37008ms pipe 3 real 0m37.010s user 0m0.001s sys 0m0.000s 1
这似乎与man ping冲突:
If ping does not receive any reply packets at all it will exit with code 1. If
a packet count and deadline are both specified,and fewer than count packets are
received by the time the deadline has arrived,it will also exit with code 1. On
other error it exits with code 2. Otherwise it exits with code 0. This makes it
possible to use the exit code to see if a host is alive or not.
1)面对ICMP错误的ping行为是否与手册页一致?在错误条件下,返回代码似乎应为2.
2)是否可以阻止我自己的机器进入这些目标主机无法访问的消息?
解决方法
我建议你从ping中渗透超时并使用timeout命令(coreutils的一部分):
timeout 300s bash -c "until ping -c10 hostother; do false; done"
如果命令超时,你将得到124作为返回码;例如如果它在5分钟内连续10次ping不成功,并且如果ping成功则为0,一旦它发生.
我知道这并没有真正回答这个问题(我承认ping手册页不是很清楚),但希望能解决你的问题.