在 bash 脚本中使用 ping 检查主机可用性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18123211/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:01:13  来源:igfitidea点击:

Checking host availability by using ping in bash scripts

bashnetworkingpingconnectivity

提问by burtek

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

我想写一个脚本,它会不断检查网络中应该整天在线的任何设备是否真的在线。我尝试使用 ping,但是

if [ "`ping -c 1 some_ip_here`" ]
then
  echo 1
else
  echo 0
fi

gives 1no 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?

给人1不管我输入有效或无效的IP地址。如何检查特定地址(或更好的 IP 地址列表中的任何设备)是否脱机?

采纳答案by StianE

Ping returns different exit codes depending on the type of error.

Ping 根据错误类型返回不同的退出代码。

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 means host reachable

0 表示主机可达

2 means unreachable

2 表示无法访问

回答by user000001

You don't need the backticks in the if statement. You can use this check

您不需要 if 语句中的反引号。您可以使用此检查

if ping -c 1 some_ip_here &> /dev/null
then
  echo 1
else
  echo 0
fi

The if command checks the exit code of the following command (the ping). If the exit code is zero (which means that the command exited successfully) the then block will be executed. If it return a non-zero exit code, then the else block will be executed.

if 命令检查以下命令(ping)的退出代码。如果退出代码为零(这意味着命令成功退出),则将执行 then 块。如果它返回非零退出代码,则将执行 else 块。

回答by Fedir RYKHTIK

There is advanced version of ping - "fping", which gives possibility to define the timeout in milliseconds.

有 ping 的高级版本 - “fping”,它提供了以毫秒为单位定义超时的可能性。

#!/bin/bash
IP='192.168.1.1'
fping -c1 -t300 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
  echo "Host found"
else
  echo "Host not found"
fi

回答by sdkks

I can think of a one liner like this to run

我能想到一个这样的班轮来运行

ping -c 1 127.0.0.1 &> /dev/null && echo success || echo fail

Replace 127.0.0.1 with IP or hostname, replace echo commands with what needs to be done in either case.

将 127.0.0.1 替换为 IP 或主机名,将 echo 命令替换为在任何一种情况下都需要执行的操作。

Code above will succeed, maybe try with an IP or hostname you know that is not accessible.

上面的代码会成功,也许可以尝试使用您知道无法访问的 IP 或主机名。

Like this:

像这样:

ping -c 1 google.com &> /dev/null && echo success || echo fail

and this

和这个

ping -c 1 lolcatz.ninja &> /dev/null && echo success || echo fail

回答by emirjonb

FYI, I just did some test using the method above and if we use multi ping (10 requests)

仅供参考,我只是使用上述方法进行了一些测试,如果我们使用多 ping(10 个请求)

ping -c10 8.8.8.8 &> /dev/null ; echo $?

ping -c10 8.8.8.8 &> /dev/null ; 回声 $?

the result of multi ping command will be "0" if at least one of ping result reachable, and "1" in case where all ping requests are unreachable.

如果 ping 结果中至少有一个可达,则 multi ping 命令的结果将为“0”,如果所有 ping 请求都不可达,则结果为“1”。

回答by miltos

up=`fping -r 1  `
if [ -z "${up}" ]; then
    printf "Host  not responding to ping   \n"
    else
    printf "Host  responding to ping  \n"
fi

回答by Alan Corey

This seems to work moderately well in a terminal emulator window. It loops until there's a connection then stops.

这似乎在终端仿真器窗口中工作得很好。它循环直到有连接然后停止。

#!/bin/bash

# ping in a loop until the net is up

declare -i s=0
declare -i m=0
while ! ping -c1 -w2 8.8.8.8 &> /dev/null ;
do
  echo "down" $m:$s
  sleep 10
  s=s+10
  if test $s -ge 60; then
    s=0
    m=m+1;
  fi
done
echo -e "--------->>  UP! (connect a speaker) <<--------" \a

The \a at the end is trying to get a bel char on connect. I've been trying to do this in LXDE/lxpanel but everything halts until I have a network connection again. Having a time started out as a progress indicator because if you look at a window with just "down" on every line you can't even tell it's moving.

最后的 \a 试图在连接时获得一个 bel 字符。我一直在尝试在 LXDE/lxpanel 中执行此操作,但一切都停止了,直到我再次建立网络连接。有一个时间开始作为一个进度指示器,因为如果你看一个窗口,每一行都只有“向下”,你甚至无法判断它正在移动。