Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3. import time
  4. import re
  5.  
  6. PATTERN = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2}$')
  7.  
  8. def subnet_to_ip_range(buff):
  9. if PATTERN.match(buff) == None:
  10. raise ValueError("Input has non IPv4 subnet line(s)")
  11. st, sub = buff.split('/')
  12. st = [int(x, 10) for x in st.split('.')]
  13. sub = 1<<(32-int(sub, 10))
  14. tot = 0
  15. for x in st:
  16. tot *= 256
  17. tot += x
  18. return str(tot)+'-'+str(tot+sub)
  19. buff = ''
  20. while True:
  21. c = sys.stdin.read(1)
  22. if c == '\n' or c == '':
  23. buff = buff.strip()
  24. if len(buff) != 0:
  25. print subnet_to_ip_range(buff)
  26. buff = ''
  27. if c == '':
  28. break
  29. buff += c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement