IP地址中自动添加缺失的0是怎么回事?(`ping 10.5` 相当于`ping 10.0.0.5`)

Nic*_*rca 36 ip hostname

我不小心打字

ssh 10.0.05
Run Code Online (Sandbox Code Playgroud)

代替

ssh 10.0.0.5
Run Code Online (Sandbox Code Playgroud)

并且非常惊讶它起作用了。我也尝试过10.00510.5并且那些也自动扩展到10.0.0.5. 我也尝试过192.168.1,扩展到192.168.0.1. 所有这些也适用于ping而不是ssh,所以我怀疑它适用于连接到任意用户提供的主机的许多其他命令。

为什么这样做?这种行为是否记录在某处?这种行为是 POSIX 的一部分还是什么?还是只是一些奇怪的实现?(使用 Ubuntu 13.10 物有所值。)

dev*_*ull 43

引自man 3 inet_aton

   a.b.c.d   Each of the four numeric parts specifies a byte of the
             address; the bytes are assigned in left-to-right order to
             produce the binary address.

   a.b.c     Parts a and b specify the first two bytes of the binary
             address.  Part c is interpreted as a 16-bit value that
             defines the rightmost two bytes of the binary address.
             This notation is suitable for specifying (outmoded) Class B
             network addresses.

   a.b       Part a specifies the first byte of the binary address.
             Part b is interpreted as a 24-bit value that defines the
             rightmost three bytes of the binary address.  This notation
             is suitable for specifying (outmoded) Class C network
             addresses.

   a         The value a is interpreted as a 32-bit value that is stored
             directly into the binary address without any byte
             rearrangement.

   In all of the above forms, components of the dotted address can be
   specified in decimal, octal (with a leading 0), or hexadecimal, with
   a leading 0X).  Addresses in any of these forms are collectively
   termed IPV4 numbers-and-dots notation.  The form that uses exactly
   four decimal numbers is referred to as IPv4 dotted-decimal notation
   (or sometimes: IPv4 dotted-quad notation).
Run Code Online (Sandbox Code Playgroud)

为了好玩,试试这个:

$ nslookup unix.stackexchange.com
Non-authoritative answer:
Name:   unix.stackexchange.com
Address: 198.252.206.140

$ echo $(( (198 << 24) | (252 << 16) | (206 << 8) | 140 ))
3338456716

$ ping 3338456716         # What?  What did we ping just now?
PING stackoverflow.com (198.252.206.140): 48 data bytes
64 bytes from 198.252.206.140: icmp_seq=0 ttl=52 time=75.320 ms
64 bytes from 198.252.206.140: icmp_seq=1 ttl=52 time=76.966 ms
64 bytes from 198.252.206.140: icmp_seq=2 ttl=52 time=75.474 ms
Run Code Online (Sandbox Code Playgroud)

  • 伙计,我希望用户在问这样的简单问题之前先尝试 [http://1249767214](http://1249767214)。 (4认同)
  • 至于为什么,它提供了一种更有用的方式来表示 A、B、C 类网络中的地址。例如,127.1 是 127.0/8 A 类环回网络中的环回地址,该网络由 127.0 到 127.0xffffff 的 1600 万个地址组成。在 192.168.0/16 B 类网络上,您的地址通常为 192.168.1 到 192.168.65534。INADDR_ANY 地址为‘0’,DHCP 广播地址 0xffffffff 更短的类型等。 (2认同)

slm*_*slm 22

除了@devnull 的好答案之外,IPv4 地址可以通过以下方式表示。

例子

该域名google.com可以通过以下方式表示:

  • 74.125.226.4  (点分十进制)
  • 1249763844  (平面小数)
  • 0112.0175.0342.0004  (虚线八进制)
  • 011237361004  (平八进制)
  • 0x4A.0x7D.0xE2.0x04  (虚线十六进制)
  • 0x4A7DE204  (平面六角)
  • 74.0175.0xe2.4  (?_?)

来源:为什么 ping 192.168.072(只有 2 个点)会返回来自 192.168.0.58 的响应?.

  • 在任何意义上,域名都不是 IPv4 地址。 (4认同)
  • 混合八进制和十进制是魔鬼的工作。 (3认同)