Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from socket import inet_aton
- from socket import inet_ntoa
- from struct import pack
- from struct import unpack
- ip2int = lambda ipstr: unpack('!I', inet_aton(ipstr))[0]
- int2ip = lambda n: inet_ntoa(pack('!I', n))
- # {'0.0.0.0' : '0.255.255.255' , 0 - 16777215
- # '10.0.0.0' : '10.255.255.255' , 167772160 - 184549375
- # '172.16.0.0' : '172.31.255.255' , 2886729728 - 2887778303
- # '192.168.0.0' : '192.168.255.255', 3232235520 - 3232301055
- # '100.64.0.0' : '100.127.255.255', 1681915904 - 1686110207
- # '127.0.0.0' : '127.255.255.255', 2130706432 - 2147483647
- # '169.254.0.0' : '169.254.255.255', 2851995648 - 2852061183
- # '192.0.0.0' : '192.0.0.255' , 3221225472 - 3221225727
- # '192.0.2.0' : '192.0.2.255' , 3221225984 - 3221226239
- # '198.51.100.0' : '198.51.100.255' , 3325256704 - 3325256959
- # '203.0.113.0' : '203.0.113.255' , 3405803776 - 3405804031
- # '192.88.99.0' : '192.88.99.255' , 3227017984 - 3227018239
- # '198.18.0.0' : '198.19.255.255' , 3323068416 - 3323199487
- # '224.0.0.0' : '239.255.255.255', 3758096384 - 4026531839
- # '240.0.0.0' : '255.255.255.255'} 4026531840 - 4294967295
- def next_ip(ip):
- ip = ip2int(ip)
- if ip >= 3758096384 - 1: return False
- elif 0 <= ip <= 16777215: return int2ip(16777215 + 1)
- elif 167772160 - 1 <= ip <= 184549375: return int2ip(184549375 + 1)
- elif 2886729728 - 1 <= ip <= 2887778303: return int2ip(2887778303 + 1)
- elif 3232235520 - 1 <= ip <= 3232301055: return int2ip(3232301055 + 1)
- elif 1681915904 - 1 <= ip <= 1686110207: return int2ip(1686110207 + 1)
- elif 2130706432 - 1 <= ip <= 2147483647: return int2ip(2147483647 + 1)
- elif 2851995648 - 1 <= ip <= 2852061183: return int2ip(2852061183 + 1)
- elif 3221225472 - 1 <= ip <= 3221225727: return int2ip(3221225727 + 1)
- elif 3221225984 - 1 <= ip <= 3221226239: return int2ip(3221226239 + 1)
- elif 3325256704 - 1 <= ip <= 3325256959: return int2ip(3325256959 + 1)
- elif 3405803776 - 1 <= ip <= 3405804031: return int2ip(3405804031 + 1)
- elif 3227017984 - 1 <= ip <= 3227018239: return int2ip(3227018239 + 1)
- elif 3323068416 - 1 <= ip <= 3323199487: return int2ip(3323199487 + 1)
- else: return int2ip(ip + 1)
- def gen_ip(ip):
- while True:
- yield next_ip(ip)
- ip = next_ip(ip)
- test = gen_ip("22.0.0.0")
- print(test.__next__())
- print(test.__next__())
- print(test.__next__())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement