Skip to content

Instantly share code, notes, and snippets.

@dlecocq
Created November 21, 2012 18:59
Show Gist options
  • Save dlecocq/4126893 to your computer and use it in GitHub Desktop.
Save dlecocq/4126893 to your computer and use it in GitHub Desktop.
Bulk Asynchronous DNS Resolution
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