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: There is also a great article on using dnspython here:

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
 Searching for 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

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

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.