Advertisement
opencard

generator of routable ip addresses

Jul 25th, 2020 (edited)
1,480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. from socket import inet_aton
  2. from socket import inet_ntoa
  3. from struct import pack
  4. from struct import unpack
  5.  
  6. ip2int = lambda ipstr: unpack('!I', inet_aton(ipstr))[0]
  7. int2ip = lambda n: inet_ntoa(pack('!I', n))
  8.  
  9. # {'0.0.0.0'     : '0.255.255.255'  ,            0 - 16777215
  10. # '10.0.0.0'     : '10.255.255.255' ,   167772160 - 184549375
  11. # '172.16.0.0'   : '172.31.255.255' , 2886729728 - 2887778303
  12. # '192.168.0.0'  : '192.168.255.255', 3232235520 - 3232301055
  13. # '100.64.0.0'   : '100.127.255.255', 1681915904 - 1686110207
  14. # '127.0.0.0'    : '127.255.255.255', 2130706432 - 2147483647
  15. # '169.254.0.0'  : '169.254.255.255', 2851995648 - 2852061183
  16. # '192.0.0.0'    : '192.0.0.255'    , 3221225472 - 3221225727
  17. # '192.0.2.0'    : '192.0.2.255'    , 3221225984 - 3221226239
  18. # '198.51.100.0' : '198.51.100.255' , 3325256704 - 3325256959
  19. # '203.0.113.0'  : '203.0.113.255'  , 3405803776 - 3405804031
  20. # '192.88.99.0'  : '192.88.99.255'  , 3227017984 - 3227018239
  21. # '198.18.0.0'   : '198.19.255.255' , 3323068416 - 3323199487
  22. # '224.0.0.0'    : '239.255.255.255', 3758096384 - 4026531839
  23. # '240.0.0.0'    : '255.255.255.255'} 4026531840 - 4294967295
  24.  
  25. def next_ip(ip):
  26.     ip = ip2int(ip)
  27.     if ip >= 3758096384 - 1: return False
  28.     elif 0 <= ip <= 16777215: return int2ip(16777215 + 1)
  29.     elif  167772160 - 1 <= ip <=  184549375: return int2ip(184549375 + 1)
  30.     elif 2886729728 - 1 <= ip <= 2887778303: return int2ip(2887778303 + 1)
  31.     elif 3232235520 - 1 <= ip <= 3232301055: return int2ip(3232301055 + 1)
  32.     elif 1681915904 - 1 <= ip <= 1686110207: return int2ip(1686110207 + 1)
  33.     elif 2130706432 - 1 <= ip <= 2147483647: return int2ip(2147483647 + 1)
  34.     elif 2851995648 - 1 <= ip <= 2852061183: return int2ip(2852061183 + 1)
  35.     elif 3221225472 - 1 <= ip <= 3221225727: return int2ip(3221225727 + 1)
  36.     elif 3221225984 - 1 <= ip <= 3221226239: return int2ip(3221226239 + 1)
  37.     elif 3325256704 - 1 <= ip <= 3325256959: return int2ip(3325256959 + 1)
  38.     elif 3405803776 - 1 <= ip <= 3405804031: return int2ip(3405804031 + 1)
  39.     elif 3227017984 - 1 <= ip <= 3227018239: return int2ip(3227018239 + 1)
  40.     elif 3323068416 - 1 <= ip <= 3323199487: return int2ip(3323199487 + 1)        
  41.     else: return int2ip(ip + 1)
  42.  
  43. def gen_ip(ip):
  44.     while True:
  45.         yield next_ip(ip)
  46.         ip = next_ip(ip)
  47.  
  48. test = gen_ip("22.0.0.0")
  49.  
  50. print(test.__next__())
  51. print(test.__next__())
  52. print(test.__next__())
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement