2014-09-03 40 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)