Generate Bind Reverse Zone File Using A Simple Python Script

When I had to create a reverse DNS zone file for my Bind name server I wanted it to be as flexible as possible and thus created a Python script that generates the Bind zone file. This makes renumbering the network really easy.

The script is available as a gist on Github:

To create the zone file, customize the configuration section in the script. Then run it and the output to stdout should be sent to the zone file:

./rdns.py > /etc/bind/db.your.zone.name

The default output (of the example config settings) would look like this:

; Zone file built with the Python Tool rdns.py:
;  This is a Python script that helps you create reverse DNS zone files for the Bind Name Server.
;  I published it in my blog post http://goo.gl/CJwly .
$TTL 1h     ; Default TTL
@   IN      SOA     ns1.example.com.        admin.example.com. (
    20120311133636  ; serial
        1h      ; slave refresh interval
        15m      ; slave retry interval
        1w      ; slave copy expire time
        1h      ; NXDOMAIN cache time
    )
; domain name servers
@   IN      NS      ns1.example.com.
; IPv6 PTR entries
1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.    IN    PTR    host1.example.com.
2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.    IN    PTR    host2.example.com.

Notes

  • The script above could also be implemented using dnslib. (Example usage of this library: dnsserver.py)
  • It was inspired by the online tool Build BIND rDNS Zone.
  • The command line tool ipv6calc can also get you the reverse nibbles notation of an IPv6 address:
    ipv6calc --out revnibbles.arpa 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Comments