Skip to content

Instantly share code, notes, and snippets.

@dustyfresh
Last active March 26, 2020 22:39
Show Gist options
  • Save dustyfresh/6803b74675a4f3067964678851fca698 to your computer and use it in GitHub Desktop.
Save dustyfresh/6803b74675a4f3067964678851fca698 to your computer and use it in GitHub Desktop.
fast DNS resolution
#!/usr/bin/env python
import json
import dns.resolver
import multiprocessing as mp
def worker(hostname, results):
resolv = dns.resolver.Resolver()
resolv.nameservers = [
'8.8.8.8', # Google
'8.8.4.4', # Google
'9.9.9.9', # Quad9
'149.112.112.112', # Quad9
'208.67.222.222', # openDNS
'208.67.220.220', # openDNS
'1.1.1.1', # cloudflare
'1.0.0.1', # cloudflare
'64.6.64.6', # verisign
'64.6.65.6', # verisign
]
records = {
'A': [],
'MX': [],
'NS': []
}
for rtype in ['A', 'MX', 'NS']:
try:
output = resolv.query(hostname, rtype)
for _ in output:
if _ not in records[rtype]:
records[rtype].append(str(_))
print('found {} answers for {} IN {}'.format(len(records[rtype]), hostname, rtype))
results.append({hostname: records})
except Exception as e:
print('{} did not answer to {}'.format(hostname))
open('./failed_resolv.txt', 'a+').write('{}\n'.format(hostname))
def main():
hostnames = open('./hostnames.txt', 'r').read().splitlines()
# Send jobs to multiprocessing pool
manager = mp.Manager()
results = manager.list() # results list
pool = mp.Pool(16)
jobs = []
for hostname in hostnames:
pool.apply_async(worker, args=(hostname, results,))
# close the pool as processing is complete
pool.close()
pool.join()
# Write output
json.dump(list(results), open('./resolv.json', 'w+'))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment