IP 주소에 누락 된 0이 자동으로 추가되는 방법은 무엇입니까? (`ping 10.5`는`ping 10.0.0.5`에 해당) 그것이 효과가 있다는 것에

실수로 입력했습니다

ssh 10.0.05

대신에

ssh 10.0.0.5

그리고 그것이 효과가 있다는 것에 매우 놀랐습니다. 나는 또한 시도 10.005하고 10.5그것들도 자동으로 확장되었습니다 10.0.0.5. 나는 또한 시도 192.168.1하고 확장했습니다 192.168.0.1. 이 모든 ping것이 오히려 오히려 작동 ssh했기 때문에 임의의 사용자 제공 호스트에 연결하는 다른 많은 명령에서 작동한다고 생각합니다.

왜 이것이 작동합니까? 이 행동은 어딘가에 기록되어 있습니까? 이 동작은 POSIX의 일부입니까? 아니면 이상한 구현입니까? (우분투에 우분투 13.10 사용하기)



답변

인용 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


답변

에 추가 @ devnull의 미세 응답, IPv4 주소는 다음과 같은 방법으로 표현 될 수있다.

이 도메인 이름 google.com은 다음과 같이 나타낼 수 있습니다.

  • 74.125.226.4  (점 분리 십진수)
  • 1249763844  (십진수)
  • 0112.0175.0342.0004  (점으로 된 8 진수)
  • 011237361004  (플랫 8 진수)
  • 0x4A.0x7D.0xE2.0x04  (점으로 구분 된 16 진수)
  • 0x4A7DE204  (플랫 육각)
  • 74.0175.0xe2.4  (ಠ_ಠ)

출처 : 핑 192.168.072 (2 개의 점만)가 192.168.0.58의 응답을 반환하는 이유는 무엇입니까? .


답변