如何将丢失的0自动添加到IP地址中?(`ping 10.5`等同于`ping 10.0.0.5`)


36

我不小心输入了

ssh 10.0.05

代替

ssh 10.0.0.5

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

为什么这样做?此行为记录在某处吗?这个行为是POSIX的一部分吗?还是只是一些怪异的实现?(使用Ubuntu 13.10的价值。)



Answers:


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).

为了娱乐,请尝试以下操作:

$ 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

2
至于为什么,它将提供一种更有用的方式来表示A,B,C类网络中的地址。例如127.1是127.0 / 8 A类回送网络中的回送地址,它包括1600万个地址127.0到127.0xffffff。在192.168.0 / 16 B类网络上,通常您的地址为192.168.1到192.168.65534。INADDR_ANY地址为0,DHCP广播地址0xffffffff较短,
以此类推

4
伙计,我希望用户在问这样的简单问题之前先尝试http:// 1249767214
blahdiblah

21

除了@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的响应?


3
八进制和十进制混合是魔鬼的工作。
Nit 2014年

4
域名在任何意义上都不是IPv4地址。
David Conrad 2014年

@DavidConrad-我认为这很明显,因为它不是数字。为那些不懂tihs的人弄清楚了。
slm
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.