Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I came across Jon Colverson's answer, and it helped me understand the dnspython module and how to process the results (I guess all DNS modules have the same twisty maze of class structure ...) I needed the TTL and the glue records, so I created my own adaptation. I am posting it here in case somebody would find it useful; I do not intend to compete with Jon Colverson's excellent answer, just fill in some additional blanks. The basic improvement is the use of name server information from the additional section of the answer, where available. I suppose a server could put something else than glue records in the additional section, so perhaps this should still be enhanced to properly correlate the information from the additional section with the information in the answer section. I also fetch and print all the name servers, not just the first one.</p> <pre><code>#!/usr/bin/env python # -*- coding: utf-8 -*- import dns.query import dns.resolver from dns.exception import DNSException def query_authoritative_ns (domain, log=lambda msg: None): default = dns.resolver.get_default_resolver() ns = default.nameservers[0] n = domain.split('.') for i in xrange(len(n), 0, -1): sub = '.'.join(n[i-1:]) log('Looking up %s on %s' % (sub, ns)) query = dns.message.make_query(sub, dns.rdatatype.NS) response = dns.query.udp(query, ns) rcode = response.rcode() if rcode != dns.rcode.NOERROR: if rcode == dns.rcode.NXDOMAIN: raise Exception('%s does not exist.' % (sub)) else: raise Exception('Error %s' % (dns.rcode.to_text(rcode))) if len(response.authority) &gt; 0: rrsets = response.authority elif len(response.additional) &gt; 0: rrsets = [response.additional] else: rrsets = response.answer # Handle all RRsets, not just the first one for rrset in rrsets: for rr in rrset: if rr.rdtype == dns.rdatatype.SOA: log('Same server is authoritative for %s' % (sub)) elif rr.rdtype == dns.rdatatype.A: ns = rr.items[0].address log('Glue record for %s: %s' % (rr.name, ns)) elif rr.rdtype == dns.rdatatype.NS: authority = rr.target ns = default.query(authority).rrset[0].to_text() log('%s [%s] is authoritative for %s; ttl %i' % (authority, ns, sub, rrset.ttl)) result = rrset else: # IPv6 glue records etc #log('Ignoring %s' % (rr)) pass return result import sys def log (msg): sys.stderr.write(msg + u'\n') for s in sys.argv[1:]: print query_authoritative_ns (s, log) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload