daily pastebin goal
80%
SHARE
TWEET

Untitled

a guest Apr 17th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import re
  3.  
  4.  
  5. def is_ipv4_addr(s):
  6.     regexp = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}' + \
  7.              '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
  8.     if re.search(regexp, s):
  9.         return True
  10.  
  11.  
  12. def is_ipv4_cidr(s):
  13.     regexp = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}' + \
  14.              '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' + \
  15.              '(\\/)(3[0-2]|[012][0-9]|[0-9]?)$'
  16.     if re.search(regexp, s):
  17.         return True
  18.  
  19.  
  20. def preflen_to_mask(preflen):
  21.     bits = 0
  22.     for i in range(32 - int(preflen), 32):
  23.         bits |= (1 << i)
  24.     return '%d.%d.%d.%d' % ((bits & 0xff000000) >> 24, \
  25.                             (bits & 0xff0000) >> 16, \
  26.                             (bits & 0xff00) >> 8, \
  27.                             (bits & 0xff))
  28.  
  29.  
  30. def ipv4_network_addr(addr, mask):
  31.     return '.'.join([str(int(octet) & int(masked)) \
  32.            for octet, masked in zip(addr.split('.'), mask.split('.'))])
  33.  
  34.  
  35. def is_network(addr, preflen):
  36.     mask = preflen_to_mask(preflen)
  37.     network = ipv4_network_addr(addr, mask)
  38.     if network == addr:
  39.         return True
  40.  
  41.  
  42. def is_ipv4_cidr_addr(s):
  43.     if not is_ipv4_cidr(s):
  44.         return False
  45.     addr, preflen = s.split('/')
  46.     if not is_network(addr, preflen):
  47.         return True
  48.  
  49.  
  50. def is_ipv4_cidr_netw(s):
  51.     if not is_ipv4_cidr(s):
  52.         return False
  53.     addr, preflen = s.split('/')
  54.     if is_network(addr, preflen):
  55.         return True
  56.  
  57.  
  58. def is_ipv4_range(s):
  59.     regexp = '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}' + \
  60.              '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' + \
  61.              '(\\-)' + \
  62.              '((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}' + \
  63.              '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
  64.     if not re.search(regexp, s):
  65.         return False
  66.     addr1, addr2 = s.split('-')
  67.     addr1 = addr1.split('.')
  68.     addr2 = addr2.split('.')
  69.     for i in range(4):
  70.         if int(addr1[i]) > int(addr2[i]):
  71.             return False
  72.     return True
  73.  
  74.  
  75. def main(arg):
  76.     if is_ipv4_addr(arg):
  77.         print('%s - ipv4 address' %arg)
  78.     elif is_ipv4_cidr_addr(arg):
  79.         print('%s - ipv4 cidr addr' %arg)
  80.     elif is_ipv4_cidr_netw(arg):
  81.         print('%s - ipv4 cidr netw' %arg)
  82.     elif is_ipv4_range(arg):
  83.         print('%s - ipv4 range' %arg)
  84.  
  85.  
  86. if __name__ == '__main__':
  87.     args = sys.argv[1:]
  88.     for arg in args:
  89.         main(arg)
RAW Paste Data
Top