Skip to content

Instantly share code, notes, and snippets.

@Azlirn
Created March 18, 2019 22:56
Show Gist options
  • Save Azlirn/3fc0e72cbb85d24e2b6139791e59784b to your computer and use it in GitHub Desktop.
Save Azlirn/3fc0e72cbb85d24e2b6139791e59784b to your computer and use it in GitHub Desktop.
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
@clairmont32
Copy link

clairmont32 commented Mar 19, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment