Python dig query

Nothing much, just a script to walk the tree for a given name. It starts at a CNAME and walks its way all the way down to the A record and IP address. In the context of larger script that attempted to connect to a given URL, we used this to troubleshoot incompatibilities between firewall timeout and DNS timeout settings.

#!/usr/bin/env python

import dns.resolver, sys
hostname = sys.argv[1]
print "issuing recursive name lookup (simulates dig)..."
n=hostname
try:
  while True:
    for rdata in dns.resolver.query(n, 'CNAME') :
      print rdata
      n=rdata.target
except:
  for rdata in dns.resolver.query(n) :
    print rdata

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.