Last active
March 26, 2020 22:39
-
-
Save dustyfresh/6803b74675a4f3067964678851fca698 to your computer and use it in GitHub Desktop.
fast DNS resolution
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
#!/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