Skip to content

Instantly share code, notes, and snippets.

@eldondev
Last active December 19, 2015 05:09
Show Gist options
  • Save eldondev/5902197 to your computer and use it in GitHub Desktop.
Save eldondev/5902197 to your computer and use it in GitHub Desktop.
A little script that uses dnspython to verify a dns migration. The first server must allow dns axfr.
import sys, difflib, pprint
import dns.resolver
my_resolver = dns.resolver.Resolver()
# 8.8.8.8 is Google's openDNS server
my_resolver.nameservers = [sys.argv[2]]
import dns.query
import dns.zone
differ = difflib.Differ()
for domain in sys.argv[3:]:
z = dns.zone.from_xfr(dns.query.xfr(sys.argv[1], domain))
names = z.nodes.keys()
names.sort()
for n in names:
for record in z[n].rdatasets:
query = '%s.' % n.to_text()
if query == '@.':
query = ''
record_text = record.to_text(n)
#print 'Asking %s for %s' % (str(my_resolver.nameservers), '%s%s' % (query, domain))
try:
answer = my_resolver.query('%s%s' % (query, domain), record.rdtype)
answer_text = answer.rrset.to_text().replace('.%s.' % domain, '')
answer_text = '\n'.join(sorted(answer_text.splitlines()))
record_text = record_text.replace('@', '%s.' % domain)
record_text = '\n'.join(sorted(record_text.splitlines()))
if record_text.lower() != answer_text.lower():
print "(%s, %s) doesn't match!" % (query, domain)
pprint.pprint(list(differ.compare(record_text.splitlines(), answer_text.splitlines())))
print "-" * 50
except dns.resolver.NoNameservers, e:
print "Can't find %s%s!" % (query, domain)
print "-" * 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment