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]

Next, we explore the module with IPython, like many other things in the book. In this example, we get the A and MX records for http://oreilly.com:

In [1]: import dns.resolver
In [2]: ip = dns.resolver.query("oreilly.com","A")
In [3]: mail = dns.resolver.query("oreilly.com","MX")
In [4]: for i,p in ip,mail:
....:     print i,p
....:     
....:     
208.201.239.37 208.201.239.36
20 smtp1.oreilly.com. 20 smtp2.oreilly.com.

In Example 14-1, we assign the “A” record results to ip and the “MX” records to mail. The “A” results are on top, and the “MX” records are on the bottom. Now ...

Get Python for Unix and Linux System Administration now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.