Last active
March 19, 2019 03:11
-
-
Save clairmont32/5c5662ee7ff1bc799494fc43d8b5e3c2 to your computer and use it in GitHub Desktop.
Improvement on https://gist.github.com/Azlirn/3fc0e72cbb85d24e2b6139791e59784b for efficiency and migrating away from Python 2.X
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment