Creating a standard zone file using dnspython

951 views
Skip to first unread message

David Okwii

unread,
Aug 25, 2016, 2:06:41 AM8/25/16
to dnspython-users
Hello, 

I've failed to get a good answer from Googling or tutorials  on how I can create a standard zone file using dnspython. Could someone kindly help with sample code on how I can generate the following zone file?

$ttl 6H
example.com.           IN SOA ns1.example.com. admin.example.com. (
                          2015111911
                          3H
                          1H
                          1W
                          6H )

;; NS Records
example.com.           IN NS ns1.example.com.
@                      IN NS ns2.example.com.
@                      IN NS ns3.example.com.
                      
;; A records

www                    IN A 192.168.1.1
mail                   IN A 192.168.1.2

I've seen seen the following sample code below, but I simply don't understand some segments of it plus it doesn't exactly generate the above zone file. Thanks. 

zone = dns.zone.Zone(dns.name.from_text('example.com'))
    all_data = (
            ('example.com.', 'SOA', ('davebeast.beast.local. david.beast.local. 2016031421 10800 3600 604800 21600',)),
            ('@', 'TXT', ('foo',)),
            ('@', 'MX', (10, 'mail')),
            ('@', 'A', ('1.2.3.4',)),
            ('mail', 'A', ('1.2.3.10',)),
        )
    for name, rtype, data in all_data:
      print name, rtype, data 
      rdtype = dns.rdatatype.from_text(rtype)
      stuff = ' '.join([str(x) for x in data])
      rdata = dns.rdata.from_text(dns.rdataclass.IN, rdtype, stuff)
      n = zone.get_rdataset(name, rdtype, create=True)
      n.add(rdata)
    zone.to_file("/etc/bind/master/example.com")

David Okwii

unread,
Aug 25, 2016, 3:37:19 AM8/25/16
to dnspython-users
Hello, 

I think I found a fair compromise. 

#I can set the TTL like this 
zone.get_rdataset(name, rdtype, create=True).add(rdata, 300)

#I can also ensure that the domain name is printed to the zone file like this 
zone.to_file("/etc/bind/master/example.com", sorted=True, relativize=True)

which gives me an output like this 

example.com. 300 IN SOA davebeast.beast.local. david.beast.local. 2016031421 10800 3600 604800 21600
example.com. 300 IN TXT "foo"
example.com. 300 IN A 1.2.3.4
mail.example.com. 300 IN A 1.2.3.10

I know this might not have been the best output, I would have loved example.com. to be substituted by "@" symbol. But I just wanted the domain name to be printed somewhere on the file, so that whoever is viewing it is aware of what they are dealing with. I think the framework can be modified to probably at least throw in a comment of the domain such as 


For readability purposes,  something like below would be great;
; example.com [448369]
$TTL 86400
@   IN  SOA ns1.linode.com. admin.example.com. 2013062147 14400 14400 1209600 86400
@       NS  ns1.linode.com.
@       NS  ns2.linode.com.
@       NS  ns3.linode.com.
@       NS  ns4.linode.com.
@       NS  ns5.linode.com.
@           MX  10  mail.example.com.
@           A   12.34.56.78
mail        A   12.34.56.78
www         A   12.34.56.78

As opposed to; 

@ 300 IN SOA davebeast.beast.local. david.beast.local. 2016031421 10800 3600 604800 21600
@ 300 IN TXT "foo"
@ 300 IN MX 10 mail
@ 300 IN A 1.2.3.4
mail 300 IN A 1.2.3.10

Purely readability suggestions. 

Peter Jakubis

unread,
Oct 10, 2016, 3:56:20 AM10/10/16
to dnspython-users
Hi David,

you can run
rndc freeze your.zone
rndc thaw your.zone

which will force BIND to rewrite the zone file to your desired format.

Maikas Katastrofa

unread,
Jul 11, 2017, 5:01:52 AM7/11/17
to dnspython-users
Cheers David. This has helped me a lot.
Reply all
Reply to author
Forward
0 new messages