5

I need to get original TTL for dns record on each query. DNS resolver shows original ttl only at first query. It shows time to reset cache on each next query.

>>> answer = dns.resolver.query('www.stackoverflow.com')
>>> print answer.rrset.ttl
300
>>> answer = dns.resolver.query('www.stackoverflow.com')
>>> print answer.rrset.ttl
292

How can I get original TTL at any query?

Paul Gear
  • 236
  • 4
  • 12
Alex Zaitsev
  • 690
  • 6
  • 17
  • you need to give time between each query, last DNS query answer stores for 20 minutes(not sure) in local Name server – Grijesh Chauhan Jun 06 '13 at 12:47
  • 1
    Try command `dig www.google.com` notice the output in `;; ANSWER SECTION:` you will get something like: `www.google.com. 218 IN A 74.125.135.99` here `218` means query result will be stored for `218` seconds at local-name-server in DNS database. Try this command again and again you can notice `218` will decreases as time pass when it decreases to `0`. – Grijesh Chauhan Jun 06 '13 at 13:08
  • @GrijeshChauhan I know it. So what is optimal way to get original ttl? I don't want to deploy own dns server with no cache for this task. Other side, dns server without cache will be very slow. – Alex Zaitsev Jun 06 '13 at 13:46
  • I don't know about `dns.resolver.query()` function there must be a return parameter for this store time, you have to give time between two consecutive calls of the function, I think no other option. – Grijesh Chauhan Jun 06 '13 at 13:52

1 Answers1

8

You can only get the original TTL by directly querying the authoritative server. This is not Python-specific.

  1. Find out what the set of authoritative nameservers is by querying for NS records for the desired name. If you find no NS records for the name then remove the first label and query again (query the parent domain). Recursively repeat until you get some NS records.
  2. Once you have NS records, query those nameservers directly for the originally requested name. In case one or more of these nameservers doesn't respond, query the next one in the list.

This is basically equivalent to doing part of the job of a recursive resolver.

Celada
  • 21,627
  • 4
  • 64
  • 78