我想要一个可以执行以下操作的 bash 脚本:
for c in computers:
do
ping $c
if ping is sucessfull:
ssh $c 'check something'
done
如果我只执行 ssh
并且计算机没有响应,则超时需要很长时间。所以我在考虑使用 ping
的输出来查看计算机是否还活着。我怎么做?其他想法也很棒
最佳答案
使用ping
的返回值:
for C in computers; do
ping -q -c 1 $C && ssh $C 'check something'
done
如果单个 ping (-c 1
) 成功,ping
将以值 0 退出。在 ping 超时时,或者如果 $C
无法解析,它将以非零值退出。
关于bash - 如何在脚本中使用 ping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1727907/