繁体   English   中英

如何同时ping多台主机(bash)linux

[英]How to ping several hosts at the same time (bash) linux

ip="192.168.129."
function addToList(){
list="$list $1"

}

addToList $1
for i in $ip{$list}
do
ping -c 1 $ip$1 > /dev/null

echo "Ping Status of $ip$1 : Success" ||
echo "Ping Status of $ip$1 : Failed"
done

如何同时 ping 多台主机并将其显示在列表中,哪个 ip 地址是 up 或 down?

一种方法是使用更强大的ping工具,如fping

另一种方法是在后台运行 ping:

for ip in $*; do
    if [[ "$ip" =~ "^[0-9]+$" ]]; then
        ip="192.168.129.$ip"
    fi

    (
        ping -c 1 $ip > /dev/null
        if [ $? -eq 0 ]; then
            echo "node $ip is up" 
        else
            echo "node $ip is down"
        fi
    )&
done

(...)&在后台运行脚本。

这是我在阅读类似帖子后编写的脚本。

https://bitbucket.org/kurtjensen/nettest/src/master/

它可以使用多个文本文件作为可能的配置,并且配置文件让您有机会更详细地命名 IP 地址。 示例配置文件是

  • home.txt - 这是默认值
  • momdad.txt - 这是给我父母的网络
  • 等等。

所以我可以在家里运行脚本,然后在提示符下按回车键或输入类似“momdad”的内容来切换到不同网络的不同配置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM