Managing a DNS server is a fairly straightforward task compared to, say, an Apache configuration file. The real problem that afflicts data centers and web hosting providers, though, is performing programatic large-scale DNS changes. It turns out that Python does quite a good job in this regard with a module called dnspython. Note there is also also another DNS module named PyDNS, but we will be covering dnspython.
To get started using dnspython, you will only need to do an easy_install as the package is listed in the Python Package Index.
ngift@Macintosh-8][H:10048][J:0]# sudo easy_install dnspython Password: Searching for dnspython Reading http://pypi.python.org/simple/dnspython/ [output supressed]
Example 14-1. Query a group of hosts
import dns.resolver hosts = ["oreilly.com", "yahoo.com", "google.com", "microsoft.com", "cnn.com"] def query(host_list=hosts): collection =  for host in host_list: ip = dns.resolver.query(host,"A”) for i in ip: collection.append(str(i)) return collection if __name__ == "__main__": for arec in query(): print arec
If we run this script, we get all of the “A” records for these hosts, and it looks like this:
[ngift@Macintosh-8][H:10046][J:0]# python query_dns.py 188.8.131.52 184.108.40.206 220.127.116.11 18.104.22.168 22.214.171.124 ...