2014-09-03 52 views
1

我真的在争取让dnspython读取区域文件,我总是被SOA束缚住,我尝试过不同区域文件和python脚本的组合。使用dnspython读取区域文件SOA错误

区域文件(来自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. 

脚本1(来自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 

的错误:

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

脚本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 

错误:

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

至于我已经下载了来自Godaddy.com,同样的问题区域文件区域文件。我从一个PRODUCTION Bind服务器(辅助区域)取得了一个区域文件,同样的错误。

所以我真的很难过,任何援助都会被大量占用。

回答

0

关于脚本1:

我也有类似的问题,因为SOA是未知的 - 因为是所有其他类型的RDATA。请使用from指令导入它们,或者输入完整的名称,例如dns.rdatatype.SOA ...

关于脚本2:

演示猜测从文件名区原点。如果文件名不是区域原点,请将该区域的原点替换为os.path.basename(filename)