1
votes

I am really battling to get dnspython to read a zone file, I always get stuck with SOA and I have tried combinations of different zones files and python scripts.

Zone file(From http://agiletesting.blogspot.com/2005/08/managing-dns-zone-files-with-dnspython.html):

$TTL 36000
example.com. IN      SOA     ns1.example.com. hostmaster.example.com. (
               2005081201      ; serial
               28800   ; refresh (8 hours)
               1800    ; retry (30 mins)
               2592000 ; expire (30 days)
               86400 ) ; minimum (1 day)

example.com.     86400   NS      ns1.example.com.
example.com.     86400   NS      ns2.example.com.
example.com.     86400   MX 10   mail.example.com.
example.com.     86400   MX 20   mail2.example.com.
example.com.     86400   A       192.168.10.10
ns1.example.com.        86400   A       192.168.1.10
ns2.example.com.        86400   A       192.168.1.20
mail.example.com.       86400   A       192.168.2.10
mail2.example.com.      86400   A       192.168.2.20
www2.example.com.       86400   A    192.168.10.20
www.example.com.        86400 CNAME     example.com.
ftp.example.com.        86400 CNAME     example.com.
webmail.example.com.    86400 CNAME     example.com.

Script 1(From http://agiletesting.blogspot.com/2005/08/managing-dns-zone-files-with-dnspython.html):

import dns.zone
from dns.exception import DNSException
import dns.ipv4
import os.path
import sys

zone_file = sys.argv[1]
print "Command line argument: " + str(zone_file)

domain = os.path.basename(zone_file).replace(".hosts","")
print "Domain - "+domain

try:
        zone = dns.zone.from_file(zone_file, domain)
        print "Zone origin:", zone.origin

        for name, node in zone.nodes.items():
                rdatasets = node.rdatasets
                print "\n**** BEGIN NODE ****"
                print "node name:", name
                for rdataset in rdatasets:
                        print "--- BEGIN RDATASET ---"
                        print "rdataset string representation:", rdataset
                        print "rdataset rdclass:", rdataset.rdclass
                        print "rdataset rdtype:", rdataset.rdtype
                        print "rdataset ttl:", rdataset.ttl
                        print "rdataset has following rdata:"
                        for rdata in rdataset:
                                print "-- BEGIN RDATA --"
                                print "rdata string representation:", rdata
                                if rdataset.rdtype == SOA:
                                        print "** SOA-specific rdata **"
                                        print "expire:", rdata.expire
                                        print "minimum:", rdata.minimum
                                        print "mname:", rdata.mname
                                        print "refresh:", rdata.refresh
                                        print "retry:", rdata.retry
                                        print "rname:", rdata.rname
                                        print "serial:", rdata.serial
                                if rdataset.rdtype == MX:
                                        print "** MX-specific rdata **"
                                        print "exchange:", rdata.exchange
                                        print "preference:", rdata.preference
                                if rdataset.rdtype == NS:
                                        print "** NS-specific rdata **"
                                        print "target:", rdata.target
                                if rdataset.rdtype == CNAME:
                                        print "** CNAME-specific rdata **"
                                        print "target:", rdata.target
                                if rdataset.rdtype == A:
                                        print "** A-specific rdata **"
                                        print "address:", rdata.address

except DNSException, e:
        print e.__class__, e

The Error:

python read_zonefile.py example.com.hosts

Command line argument: example.com.hosts

Domain - example.com

Zone origin: example.com.

** BEGIN NODE **

node name: @

--- BEGIN RDATASET ---

rdataset string representation: 36000 IN SOA ns1 hostmaster 2005081201 28800 1800 2592000 86400

rdataset rdclass: 1

rdataset rdtype: 6

rdataset ttl: 36000

rdataset has following rdata:

-- BEGIN RDATA --

rdata string representation: ns1 hostmaster 2005081201 28800 1800 2592000 86400

Traceback (most recent call last):

File "read_zonefile.py", line 31, in

if rdataset.rdtype == SOA:

NameError: name 'SOA' is not defined

Script 2:

import dns.zone
import dns.ipv4
import os.path
import sys
reverse_map Olivia Munn= {}
for filename in sys.argv[1:]:
        zone = dns.zone.from_file(filename, os.path.basename(filename),relativize=False)
        for (name, ttl, rdata) in zone.iterate_rdatas('A'):
                try:
                        reverse_map[rdata.address].append(name.to_text())
                except KeyError:
                        reverse_map[rdata.address] = [name.to_text()]
keys = reverse_map.keys()
keys.sort(lambda a1, a2: cmp(dns.ipv4.inet_aton(a1), dns.ipv4.inet_aton(a2)))
for k in keys:
        v = reverse_map[k]
        v.sort()
        print k, v

Error:

python create_rev_dns.py example.com.hosts

Traceback (most recent call last):

File "create_rev_dns.py", line 7, in

zone = dns.zone.from_file(filename, os.path.basename(filename),relativize=False)

File "/usr/lib64/python2.6/site-packages/dns/zone.py", line 977, in from_file

filename, allow_include, check_origin)

File "/usr/lib64/python2.6/site-packages/dns/zone.py", line 924, in from_text

reader.read()

File "/usr/lib64/python2.6/site-packages/dns/zone.py", line 882, in read

self.zone.check_origin()

File "/usr/lib64/python2.6/site-packages/dns/zone.py", line 521, in check_origin

raise NoSOA

dns.zone.NoSOA

As for the Zone file I have downloaded a zone file from Godaddy.com, the same issue. I have taken a zone file from a PRODUCTION Bind server(Secondary Zone), same error.

So I am really stumped, any assistance will greatly be appropriated.

1

1 Answers

0
votes

Concerning script 1:

I had a similar issue, since SOA is unknown - as are all the other rdata types. Either import them using the from directive, or give the full name, e.g. dns.rdatatype.SOA, ...

Concerning script 2:

The demo guesses the zone origin from the filename. If the filename is not the zone origin, replace os.path.basename(filename) by the origin of the zone.