通过在 bash 脚本中使用 ping 检查主机可用性 [英] Checking host availability by using ping in bash scripts
本文介绍了通过在 bash 脚本中使用 ping 检查主机可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写一个脚本,它会不断检查网络中应该整天在线的任何设备是否真的在线.我尝试使用 ping,但是
I want to write a script, that would keep checking if any of the devices in network, that should be online all day long, are really online. I tried to use ping, but
if [ "`ping -c 1 some_ip_here`" ]
then
echo 1
else
echo 0
fi
给出 1
无论我输入有效或无效的 IP 地址.如何检查特定地址(或更好的 IP 地址列表中的任何设备)是否脱机?
gives 1
no matter if I enter valid or invalid ip address. How can I check if a specific address (or better any of devices from list of ip addresses) went offline?
推荐答案
Ping 根据错误类型返回不同的退出代码.
Ping returns different exit codes depending on the type of error.
ping 256.256.256.256 ; echo $?
# 68
ping -c 1 127.0.0.1 ; echo $?
# 0
ping -c 1 192.168.1.5 ; echo $?
# 2
0 表示主机可达
2 表示无法访问
这篇关于通过在 bash 脚本中使用 ping 检查主机可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文