通过的bash脚本使用ping检查主机可用性 [英] Checking host availability by using ping in bash scripts

查看:173
本文介绍了通过的bash脚本使用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?

推荐答案

平安返回不同的退出codeS根据错误的类型。

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表示主机可达

0 means host reachable

2,表示不可达。

这篇关于通过的bash脚本使用ping检查主机可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆