Created
March 18, 2019 22:56
-
-
Save Azlirn/3fc0e72cbb85d24e2b6139791e59784b to your computer and use it in GitHub Desktop.
This file contains 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
from netaddr import * | |
import pprint | |
import socket | |
data = raw_input("Enter the CIDR you would like me to resolve: ") | |
network = IPNetwork(data) | |
file = open ("hostnames.txt", "a") | |
for ip in network: | |
resolveip = str(ip) | |
try: | |
socket.inet_aton(resolveip) | |
except Exception as invalid_ip: | |
print "%s did not resolve" % invalid_ip | |
else: | |
hostname = socket.getfqdn(resolveip) | |
if hostname == resolveip: | |
pass | |
print resolveip + " - No Hostname Found" | |
else: | |
file.write(hostname + "\n") | |
print hostname |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As discussed offline, here's a refactor for efficiency and migration to Python 3-
https://gist.github.com/clairmont32/5c5662ee7ff1bc799494fc43d8b5e3c2
Keep in mind if you're expanding a massive network, this could be even more efficient using threads with each thread assigned to process X number of hosts.