Created
February 15, 2012 11:40
-
-
Save kgaughan/1835224 to your computer and use it in GitHub Desktop.
Getting the domains assigned to a bunch of domains from DNS
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
from dns.resolver import Resolver, query, NXDOMAIN | |
import dns.query | |
from dns.exception import Timeout | |
from dns.message import make_query | |
from dns.rdatatype import NS | |
import socket | |
from random import choice | |
def get_zone_nameservers(zone): | |
nss = [] | |
for answer in query(zone, 'NS'): | |
nss.append(socket.gethostbyname(answer.to_text())) | |
return nss | |
def names_to_nameservers(names): | |
zone_nss = {} | |
for name in names: | |
domain, zone = name.split('.', 1) | |
if zone not in zone_nss: | |
zone_nss[zone] = get_zone_nameservers(zone + '.') | |
msg = make_query(name + '.', NS) | |
ns = choice(zone_nss[zone]) | |
response = dns.query.udp(msg, ns) | |
yield (name, tuple(sorted(rr.target.to_text() for rr in response.authority[0] if rr.rdtype == NS))) | |
with open('domains.txt', 'r') as fh: | |
for host, nss in names_to_nameservers(line.rstrip() for line in fh): | |
if nss == (): | |
ns = "NONE" | |
else: | |
ns = "\n\t".join(nss) | |
print host + "\n\t" + ns + "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Somewhat rough and ready, I know, but useful nonetheless.