ping 发送 0 个字节

vnc*_*vnc 7 linux ping

Linux(CentOS)中的ping命令是否可以发送0字节。在 Windows 中可以定义使用-l参数
命令尝试

  ping localhost -s 0
    PING localhost (127.0.0.1) 0(28) bytes of data.
    8 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64
    8 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64
    ^C
    --- localhost ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 999ms



man ping

-s packetsize
              Specifies  the  number of data bytes to be sent.  The default is
              56, which translates into 64 ICMP data bytes when combined  with
              the 8 bytes of ICMP header data.
Run Code Online (Sandbox Code Playgroud)

Edit1:添加 ping 的 windows 输出以防万一有人需要它

ping 127.0.0.1  -l 0

Pinging 127.0.0.1 with 0 bytes of data:
Reply from 127.0.0.1: bytes=0 time<1ms TTL=128
Reply from 127.0.0.1: bytes=0 time<1ms TTL=128
Reply from 127.0.0.1: bytes=0 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

ping 127.0.0.1

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
Run Code Online (Sandbox Code Playgroud)

cas*_*sey 27

在 Linux、Windows 或任何其他声称能够发送 ping 的平台上,ping 不能为 0 字节。至少该数据包必须包含一个 IP 标头,并且一个非畸形的无技巧播放 ping 还将包括一个 ICMP 标头,它是 8 个字节长。

窗口可能在输出接收到的字节方面有所不同。Linux 告诉您数据包的 ICMP 部分的大小(ICMP 标头加上任何存在的 ICMP 数据的 8 个字节)。Windows 可能会打印 ICMP 有效负载数据字节的数量,以便在它告诉您“0”时,那 8 个 ICMP 标头字节仍然存在。要真正拥有 0 ICMP 字节,这意味着您的数据包是原始 IP 标头,不再是 ICMP ping 请求。关键是,即使 Windows 告诉您 ping 数据包的长度为 0 字节,也不是。

ICMP 回显请求或回显回复数据包的最小大小为 28 字节:

  • 20 字节 IP 标头,
  • 4字节ICMP报头,
  • 4 字节回显请求/回复头数据,
  • 0 字节的 ICMP 有效载荷数据。

在 linux 上 ping 时打印:

8 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64
Run Code Online (Sandbox Code Playgroud)

这 8 个字节是 4 字节 ICMP 报头和 4 字节 ICMP 回显回复报头数据,反映了 0 字节的 ICMP 有效载荷数据大小。


Cri*_*gie 11

你的第 2 层传输介质是什么?以太网?无论承载什么,都无法让以太网帧短于 64 字节。

因此,即使您可以发送带有 0 字节有效载荷的 ICMP ping,您的以太网卡也会将帧填充至 64 字节以进行传输。

@Casey 表明您的理论最小值为 28 字节,但以太网将添加 36 字节的填充以使其总数达到 64 字节。少的是一个 Runt 数据包,而且太短了。

为什么是 64 字节?这是一个全新的问题,与比特的传输时间和冲突检测有关,以太网是 CSMA/CD 域而不是 CSMA/CA。


rex*_*ans 5

您必须区分标头大小和有效负载大小(数据)。ICMP 数据包可以选择保存数据。标头的大小恒定为 64 位,即 8 个字节。有效负载可能会有所不同,甚至可能不存在。这意味着发送 0 字节,并在man-page中进行了准确描述ping

  -s packetsize
          Specifies the number of data bytes to be sent.  The default is 56, which translates into 64 ICMP data bytes when combined with the 8 bytes of ICMP header data.
Run Code Online (Sandbox Code Playgroud)

当您通过将数据包大小设置为 0 来执行时,它会输出以下内容:

ping -s 0 192.168.172.51
PING 192.168.172.51 (192.168.172.51) 0(28) bytes of data.
8 bytes from 192.168.172.51: icmp_seq=1 ttl=63
8 bytes from 192.168.172.51: icmp_seq=2 ttl=63
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它确实提到了 8 个字节 - 计算标头和有效负载。您可以使用任意数据包大小进行进一步测试,并看到总会多传输 8 个字节,因为这是附加标头的恒定大小。

同时tcpdump证明了这一点:

11:49:30.271160 IP 192.168.172.50 > 192.168.172.51: ICMP echo request, id 5472, seq 30, length 8
Run Code Online (Sandbox Code Playgroud)

标头行中的 28 个额外字节 0(28) 来自 IP+ICMP 标头。