Created
November 21, 2012 18:59
-
-
Save dlecocq/4126893 to your computer and use it in GitHub Desktop.
Bulk Asynchronous DNS Resolution
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 struct | |
from gevent import pool | |
from gevent.dns import resolve_ipv4 | |
# Should be a file with one hostname per line | |
with open('hosts') as fin: | |
urls = fin.read().split('\n') | |
def func(host): | |
try: | |
print 'Resolving %s' % host | |
return resolve_ipv4(host)[1] | |
except: | |
return [] | |
p = pool.Pool(100) | |
results = p.map(func, urls) | |
unpacked = [] | |
for r in results: | |
out = ['.'.join(str(s) for s in struct.unpack('!BBBB', o)) for o in r] | |
unpacked.append(out) | |
matches = [] | |
# The ip we're trying to find | |
to_match = '64.70.56.99' | |
for host, ips in izip(urls, unpacked): | |
if to_match in ips: | |
print '%s => %s' % (host, ips) | |
matches.append(host) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment