Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import re
  2. import socket
  3. import ipaddress
  4.  
  5. # gather range from user
  6. cidrNetwork = str(input('Enter a CIDR range to resolve:\n'))
  7.  
  8. # exit if input range does not match a (basic) valid IP/notation format
  9. while True:
  10. if not re.fullmatch('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,}', cidrNetwork):
  11. exit('Enter a valid CIDR range.\n')
  12. break
  13.  
  14. # explode the network into hosts
  15. try:
  16. hosts = [ip for ip in ipaddress.ip_network(cidrNetwork).hosts()]
  17. except Exception as err:
  18. print(err)
  19. exit()
  20.  
  21.  
  22. # do a basic resolution, append non-initial values
  23. resolved = []
  24. for x in hosts:
  25. x = str(x)
  26. reverse = socket.getfqdn(x)
  27. if reverse is not x:
  28. resolved.append(reverse + '\n')
  29.  
  30.  
  31. print('Writing results to file...')
  32.  
  33. # do a basic write and error check. This could be more exception safe but will do
  34. try:
  35. with open('resolved_hosts.txt', 'wt') as file:
  36. file.write(''.join(resolved))
  37.  
  38. except Exception as Err:
  39. print(Err)
  40.  
  41. print('Done')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement