web-dev-qa-db-ja.com

不足している0がIPアドレスに自動的に追加されるのはなぜですか? ( `ping 10.5`は` ping 10.0.0.5`と同等)

誤って入力した

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を使用する価値があります。)

36
Nicu Stiurca

引用 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
43
devnull

@ devnull's に加えて、IPv4アドレスは次の方法で表すことができます。

このドメイン名、google.comは、次の方法で表すことができます。

  • 74.125.226.4(ドット付き10進数)
  • 1249763844(フラット10進数)
  • 0112.0175.0342.0004(ドット付き8進数)
  • 011237361004(フラット8進数)
  • 0x4A.0x7D.0xE2.0x04(ドット付き16進数)
  • 0x4A7DE204(平角)
  • 74.0175.0xe2.4(ಠ_ಠ)

ソース:192.168.072にpingを送信する理由2ドットのみ)192.168.0.58から応答を返しますか?

21
slm