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
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 : import dns.resolver In : ip = dns.resolver.query("oreilly.com","A") In : mail = dns.resolver.query("oreilly.com","MX") In : for i,p in ip,mail: ....: print i,p ....: ....: 184.108.40.206 220.127.116.11 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 ...