Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import socket
- import ipaddress
- # gather range from user
- cidrNetwork = str(input('Enter a CIDR range to resolve:\n'))
- # exit if input range does not match a (basic) valid IP/notation format
- while True:
- if not re.fullmatch('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,}', cidrNetwork):
- exit('Enter a valid CIDR range.\n')
- break
- # explode the network into hosts
- try:
- hosts = [ip for ip in ipaddress.ip_network(cidrNetwork).hosts()]
- except Exception as err:
- print(err)
- exit()
- # do a basic resolution, append non-initial values
- resolved = []
- for x in hosts:
- x = str(x)
- reverse = socket.getfqdn(x)
- if reverse is not x:
- resolved.append(reverse + '\n')
- print('Writing results to file...')
- # do a basic write and error check. This could be more exception safe but will do
- try:
- with open('resolved_hosts.txt', 'wt') as file:
- file.write(''.join(resolved))
- except Exception as Err:
- print(Err)
- print('Done')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement