Linux

ping 發送 0 個字節

  • January 5, 2022

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.

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

在 Linux、Windows 或任何其他聲稱能夠發送 ping 的平台上,ping 不能是 0 字節。至少數據包必須包含一個 IP 標頭,並且非格式錯誤的無技巧播放 ping 還將包含一個 8 字節長的 ICMP 標頭。

windows 輸出接收到的字節的方式可能有所不同。Linux 會告訴您數據包的 ICMP 部分的大小(ICMP 標頭的 8 個字節加上任何存在的 ICMP 數據)。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

這 8 個字節是 4 字節 ICMP 報頭和 4 字節 ICMP 回應答覆報頭數據,反映了 0 字節的 ICMP 有效負載數據大小。

引用自:https://unix.stackexchange.com/questions/234832