A 32-bit IPv4 address is divided into four octets. Each octet is represented numerically in decimal, using the minimum possible number of digits (leading zeroes are not used, exceptin the case of 0 itself). The four encoded octets are given most-significant first, separated by period characters.
octet-dot-octet-dot-16bits, intended for class B addresses
octet-dot-24bits, intended for class A addresses.
It also allowed some flexibility in how the individual numeric parts were specified. it allowed octal and hexadecimal in addition to decimal, distinguishing these radices by using the C language syntax involving a prefix “0” or “0x”, and allowed the numbers to be arbitrarily long.
# coding:utf8 # by https://findneo.github.io/ # ref: https://linux.die.net/man/3/inet_aton # https://tools.ietf.org/html/draft-main-ipaddr-text-rep-02 # https://tools.ietf.org/html/rfc3986 # http://www.linuxsa.org.au/pipermail/linuxsa/2007-September/088131.html import itertools as it import random ip = '192.168.66.233' i = ip.split('.')
deff(x): returnhex(int(x))[2:].zfill(2)
hi = [f(i[0]), f(i[1]), f(i[2]), f(i[3]), # hi[4]:part c of "a.b.c" f(i[2]) + f(i[3]), # hi[5]:part b of "a.b" f(i[1]) + f(i[2]) + f(i[3]), # hi[6]:'a' f(i[0]) + f(i[1]) + f(i[2]) + f(i[3]), ]
p = [hex2hex, hex2int, hex2oct] res = [] # "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. res.extend(['.'.join([i[0](hi[0]), i[1](hi[1]), i[2](hi[2]), i[3](hi[3])]) for i in it.product(p, p, p, p)])
# "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. res.extend(['.'.join([i[0](hi[0]), i[1](hi[1]), i[2](hi[4])]) for i in it.product(p, p, p)])
# "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. res.extend(['.'.join([i[0](hi[0]), i[1](hi[5])]) for i in it.product(p, p)])
# "a" # The value a is interpreted as a 32-bit value that is stored directly into the binary address without any byte rearrangement. res.extend(['.'.join([i[0](hi[6])]) for i in it.product(p)]) for i in xrange(len(res)): print"[%d]\t%s" % (i, res[i])
# ------------------------------------------------------------------------------- # test import os
except_ip = []
deftest_notation(ip_notation): global except_ip x = os.popen('ping -n 1 -w 0.5 ' + ip_notation).readlines() answer = x[0] iflen(x) == 1else x[1] if ip notin answer: except_ip.append(ip_notation) return answer.decode('gbk').strip()
print"\nchecking. . .", for i in xrange(len(res)): # print "[%d] %s\t\t\t%s" % (i, res[i], test_notation(res[i])) test_notation(res[i]) print'.',
print"\n\ntotally %d notations of ip checked ,all are equivalent to %s" % (len(res), ip) iflen(except_ip): print"except for notations following:\n", except_ip