Copyright by Jeremy M. Jones, Noah Gift

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Chapter 14. Pragmatic Examples

Managing DNS with Python

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.

Make sure you refer to the official documentation: http://www.dnspython.org/. There is also a great article on using dnspython here: http://vallista.idyll.org/~grig/articles/.

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 208.201.239.37 208.201.239.36 216.109.112.135 66.94.234.13 64.233.167.99 ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required