如何同时 ping 多个 IP 地址?

如何同时 ping 多个 IP 地址?

我知道可以运行 Bashfor循环和ping多个服务器的方法,是否有一个我可以使用的 Linux CLI 工具,它允许我执行此操作,而无需将 Bash 脚本写入ping服务器列表一次一个?

像这样的东西:

$ ping host1 host2 host3

笔记:我正在专门寻找 CentOS/Fedora,但如果它适用于其他发行版,那也很好。

答案1

如果你调查NMAP项目您会发现它除了以下工具之外还包含其他工具nmap。这些工具之一是nping,其中包括以下能力:

Nping 具有非常灵活且功能强大的命令行界面,使用户能够完全控制生成的数据包。 Nping 的功能包括:

  • 自定义 TCP、UDP、ICMP 和 ARP 数据包生成。
  • 支持多种目标主机规范。
  • 支持多个目标端口规范。
  • ...

nping位于标准 EPEL 存储库中进行启动。

$ repoquery -qlf nmap.x86_64 | grep nping
/usr/bin/nping
/usr/share/man/man1/nping.1.gz

用法

要 ping 多个服务器,您只需告知nping名称/IP 以及您要使用的协议。由于我们想要模仿传统pingCLI 的功能,因此我们将使用 ICMP。

$ sudo nping -c 2 --icmp scanme.nmap.org google.com

Starting Nping 0.7.70 ( https://nmap.org/nping ) at 2019-06-14 13:43 EDT
SENT (0.0088s) ICMP [10.3.144.95 > 45.33.32.156 Echo request (type=8/code=0) id=42074 seq=1] IP [ttl=64 id=57921 iplen=28 ]
RCVD (0.0950s) ICMP [45.33.32.156 > 10.3.144.95 Echo reply (type=0/code=0) id=42074 seq=1] IP [ttl=46 id=24195 iplen=28 ]
SENT (1.0091s) ICMP [10.3.144.95 > 45.33.32.156 Echo request (type=8/code=0) id=42074 seq=2] IP [ttl=64 id=57921 iplen=28 ]
SENT (2.0105s) ICMP [10.3.144.95 > 45.33.32.156 Echo request (type=8/code=0) id=42074 seq=2] IP [ttl=64 id=57921 iplen=28 ]
RCVD (2.0107s) ICMP [45.33.32.156 > 10.3.144.95 Echo reply (type=0/code=0) id=42074 seq=2] IP [ttl=46 id=24465 iplen=28 ]
SENT (3.0138s) ICMP [10.3.144.95 > 64.233.177.100 Echo request (type=8/code=0) id=49169 seq=2] IP [ttl=64 id=57921 iplen=28 ]

Statistics for host scanme.nmap.org (45.33.32.156):
 |  Probes Sent: 2 | Rcvd: 2 | Lost: 0  (0.00%)
 |_ Max rtt: 86.053ms | Min rtt: 0.188ms | Avg rtt: 43.120ms
Statistics for host google.com (64.233.177.100):
 |  Probes Sent: 2 | Rcvd: 0 | Lost: 2  (100.00%)
 |_ Max rtt: N/A | Min rtt: N/A | Avg rtt: N/A
Raw packets sent: 4 (112B) | Rcvd: 2 (108B) | Lost: 2 (50.00%)
Nping done: 2 IP addresses pinged in 3.01 seconds

我发现此工具的唯一缺点是使用 ICMP 模式需要 root 权限。

$ nping -c 2 --icmp scanme.nmap.org google.com
Mode ICMP requires root privileges.

答案2

位于同名的 Fedora 包中,允许多个主机或一组 IP 地址。

$ fping -a -A -c 1 hosta hostb
192.168.0.20 : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.64/0.64/0.64
192.168.1.3  : xmt/rcv/%loss = 1/1/0%, min/avg/max = 0.50/0.50/0.50

fping 将发送一个 ping 数据包并以循环方式移动到下一个目标...如果目标回复,则会记录下来并将其从列表中删除

答案3

我建议使用GNU 并行

parallel -u ping ::: host1 host2 host3

输出将被交错

答案4

我知道具体是不是你所要求的,而是一个 bash 脚本来完成这个任务:

#!/bin/bash

for host; do
    ping -c5 "$host" 2>&1 | tail -3 &
done

wait

这会将您的端点作为命令行参数,并向每个端点发送 5 次 ping 作为后台进程,然后等待所有端点完成后再退出。它将打印 ping 输出的最后三行,其中包含有关成功率和延迟的有用统计信息。

相关内容