bash 如何在脚本中使用 ping
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1727907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use ping in a script
提问by Guy
I want a bash script that'll do:
我想要一个可以执行以下操作的 bash 脚本:
for c in computers:
do
ping $c
if ping is sucessfull:
ssh $c 'check something'
done
If I only do ssh
and the computer is iresponsive, it takes forever for the timeout. So I was thinking of using the output of ping
to see if the computer is alive or not. How do I do that? Other ideas will be great also
如果我只这样做ssh
并且计算机没有响应,则超时需要永远。所以我在考虑使用 的输出ping
来查看计算机是否还活着。我怎么做?其他想法也会很棒
回答by Stephan202
Use ping
's return value:
使用ping
的返回值:
for C in computers; do
ping -q -c 1 $C && ssh $C 'check something'
done
ping
will exit with value 0 if that single ping (-c 1
) succceeds. On a ping timeout, or if $C
cannot be resolved, it will exit with a non-zero value.
ping
如果单个 ping ( -c 1
) 成功,则将以 0 值退出。在 ping 超时或$C
无法解析时,它将以非零值退出。
回答by Mike Mazur
Use the -w
switch (or -t
on FreeBSD and OS X) on the ping
command, then inspect the command's return value.
在命令上使用-w
开关(或-t
在 FreeBSD 和 OS X 上)ping
,然后检查命令的返回值。
ping -w 1 $c
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
ssh $c 'check something'
fi
You may want to adjust the parameter you pass with -w
if the hosts you're connecting to are far away and the latency is higher.
-w
如果您连接的主机距离较远且延迟较高,您可能需要调整传递的参数。
From man ping
:
来自man ping
:
-w deadline
Specify a timeout, in seconds, before ping exits regardless of
how many packets have been sent or received. In this case ping
does not stop after count packet are sent, it waits either for
deadline expire or until count probes are answered or for some
error notification from network.
回答by kkeller
Not all network environments allow ping to go through (though many do) and not all hosts will answer a ping request. I would recommend not to use ping, but instead set the connect timeout for ssh:
并非所有网络环境都允许 ping 通(尽管很多都允许)并且并非所有主机都会响应 ping 请求。我建议不要使用 ping,而是为 ssh 设置连接超时:
for c in compuers; do ssh -o ConnectTimeout=2 $c 'check something' done
回答by Hatef
Using 64 value as measurement tool is not logical. It is better to use the number of received/lost packets instead.
使用 64 值作为测量工具是不合逻辑的。最好使用接收/丢失数据包的数量。
This script would work:
这个脚本可以工作:
RESULT="1"
PING=$(ping ADDRESS -c 1 | grep -E -o '[0-9]+ received' | cut -f1 -d' ')
if [ "$RESULT" != "$PING" ]
then
DO SOMETHING
else
DO SOMETHING
fi
回答by smRaj
Here's my hack:
这是我的黑客:
#ipaddress shell variable can be provided as an argument to the script.
while true
do
nmap_output=$(nmap -p22 ${ipaddress})
$(echo ${nmap_output} | grep -q open)
grep_output=$?
if [ "$grep_output" == 0 ] ; then
#Device is LIVE and has SSH port open for clients to connect
break
else
#[01 : bold
#31m : red color
#0m : undo text formatting
echo -en "Device is \e[01;31mdead\e[0m right now .... !\r"
fi
done
#3[K : clear the text for the new line
#32 : green color
echo -e "3[KDevice is \e[01;32mlive\e[0m !"
ssh user@${ipaddress}
Doesn't rely on just ping
. Why ?
- Succesful ping
doesn't guarantee you a successful ssh
access. You could still add ping
test as well, to the beginning of this script and exit if ping fails and do nothing from the above.
不仅仅依赖于ping
. 为什么 ?
-ping
成功并不能保证您ssh
访问成功。您仍然可以ping
在此脚本的开头添加test 并在 ping 失败时退出,并且不执行上述操作。
Above bash
script snippet, verifies if the device you are trying to
access has the SSH port open for clients(you) to connect to. Requires nmap
package to be installed.
上面的bash
脚本片段验证您尝试访问的设备是否为客户端(您)打开了 SSH 端口以进行连接。需要nmap
安装包。
I don't understand why you want to ssh
into multiple computers in that script. But, mine works for ssh into one device and can be modified to suit your needs.
我不明白您为什么要ssh
在该脚本中进入多台计算机。但是,我的 ssh 可以在一台设备中使用,并且可以根据您的需要进行修改。
回答by Paul Wenzel
回答by reinierpost
I wrote that script some 10 years ago:
大约 10 年前我写了那个脚本:
http://www.win.tue.nl/~rp/bin/rshall
http://www.win.tue.nl/~rp/bin/rshall
You probably won't need the part where it determines every reachable host and iterates over each of them.
您可能不需要确定每个可访问主机并遍历每个主机的部分。
回答by Space Rocker
Use this in your bash loop:
在 bash 循环中使用它:
RESULT="64"
PING=$(ping 127.0.0.1 -c 1 | grep 64 | awk '{print }')
if [ "$RESULT" != "$PING" ]
then
#ping failed
else
#ping successful, do ssh here
fi