ping 结果异常

ping 结果异常

当我在 Ubuntu 机器上运行这个程序时:

sudo ping google.com -c 25  -l 25

尽管它应该(如果我没看错的话)一开始就发送所有 25 个数据包,但我每 2 秒就会收到 1 行回复。奇怪的是,它声称我得到的 ping 时间为:

--- google.com ping statistics ---
25 packets transmitted, 24 received, 4% packet loss, time 0ms
rtt min/avg/max/mdev = 23.370/28.067/32.902/2.895 ms, pipe 24

另外,如果我想提前退出,我需要为每个数据包按一次 ctrl-C。有人知道是什么原因造成的吗?

答案1

如果您通过 IP 而不是主机名进行 ping,您将收到预期的行为。

我仍在调查以找出出现这种情况的根本原因。

# find IP address of host
$ host -t a google.com
google.com has address 74.125.225.17
google.com has address 74.125.225.19
google.com has address 74.125.225.20
google.com has address 74.125.225.18
google.com has address 74.125.225.16

# pick an IP and ping it, all output is displayed at once
$ sudo ping 74.125.225.17 -c 25 -l 25
PING 74.125.225.17 (74.125.225.17) 56(84) bytes of data.
64 bytes from 74.125.225.17: icmp_seq=1 ttl=55 time=29.7 ms
64 bytes from 74.125.225.17: icmp_seq=2 ttl=55 time=30.4 ms
64 bytes from 74.125.225.17: icmp_seq=3 ttl=55 time=40.0 ms
64 bytes from 74.125.225.17: icmp_seq=4 ttl=55 time=40.4 ms
64 bytes from 74.125.225.17: icmp_seq=7 ttl=55 time=50.1 ms
64 bytes from 74.125.225.17: icmp_seq=5 ttl=55 time=50.4 ms
64 bytes from 74.125.225.17: icmp_seq=6 ttl=55 time=51.4 ms
64 bytes from 74.125.225.17: icmp_seq=8 ttl=55 time=52.4 ms
64 bytes from 74.125.225.17: icmp_seq=9 ttl=55 time=55.4 ms
64 bytes from 74.125.225.17: icmp_seq=10 ttl=55 time=56.4 ms
64 bytes from 74.125.225.17: icmp_seq=11 ttl=55 time=57.3 ms
64 bytes from 74.125.225.17: icmp_seq=13 ttl=55 time=58.3 ms
64 bytes from 74.125.225.17: icmp_seq=12 ttl=55 time=59.3 ms
64 bytes from 74.125.225.17: icmp_seq=14 ttl=55 time=60.3 ms
64 bytes from 74.125.225.17: icmp_seq=15 ttl=55 time=61.9 ms
64 bytes from 74.125.225.17: icmp_seq=16 ttl=55 time=62.3 ms
64 bytes from 74.125.225.17: icmp_seq=17 ttl=55 time=63.2 ms
64 bytes from 74.125.225.17: icmp_seq=18 ttl=55 time=64.2 ms
64 bytes from 74.125.225.17: icmp_seq=19 ttl=55 time=68.9 ms
64 bytes from 74.125.225.17: icmp_seq=20 ttl=55 time=69.2 ms
64 bytes from 74.125.225.17: icmp_seq=21 ttl=55 time=70.2 ms
64 bytes from 74.125.225.17: icmp_seq=22 ttl=55 time=75.9 ms
64 bytes from 74.125.225.17: icmp_seq=23 ttl=55 time=76.2 ms
64 bytes from 74.125.225.17: icmp_seq=24 ttl=55 time=77.2 ms
64 bytes from 74.125.225.17: icmp_seq=25 ttl=55 time=78.1 ms

更新

通过 strace 运行 ping 后,我发现它卡在了名称解析上(这并不奇怪)。然而,引起我注意的是avahi-daemon。此服务实现了 Apple 的 Zeroconf 架构(也称为“Rendezvous”或“Bonjour”)。换句话说,我不需要这个功能。

停止 avahi-daemon 之后,ping 行为恢复正常。

# sudo /etc/init.d/avahi-daemon stop

可以通过以下方式在启动期间禁用它:

# sudo update-rc.d -f avahi-daemon

另一个解决方法是简单地使用-n带有 ping 的标志。挂断是由于在处理回复时执行了反向 DNS 查找。

答案2

如果您在执行 ping 时执行 tcpdump,您可能会看到它在反向映射查找上暂停...即:当它查找 IP 到名称的 DNS 映射时。

我猜想当数据包返回时,avahi-daemon 正在链中的某个地方被查询。-l 25 只关心出站数据包...无论你是否预加载,ping 仍然会执行其工作并查找所有名称。:)

相关内容