我真的很想让dnspython读取一个区域文件,我总是被SOA困住,我尝试过不同区域文件和python脚本的组合。
区域文件(来自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
命令行参数:example.com.hosts
域名 - example.com
区域来源:example.com。
** BEGIN NODE **
节点名称:@
--- BEGIN RDATASET ---
rdataset字符串表示形式:36000 IN SOA ns1 hostmaster 2005081201 28800 1800 2592000 86400
rdataset rdclass:1
rdataset rdtype:6
rdataset ttl:36000
rdataset有以下rdata:
- BEGIN RDATA -
rdata字符串表示形式:ns1 hostmaster 2005081201 28800 1800 2592000 86400
追踪(最近一次呼叫最后一次):
文件“read_zonefile.py”,第31行,
if rdataset.rdtype == SOA:
NameError:名称'SOA'未定义
脚本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
追踪(最近一次呼叫最后一次):
文件“create_rev_dns.py”,第7行,
zone = dns.zone.from_file(filename, os.path.basename(filename),relativize=False)
文件“/usr/lib64/python2.6/site-packages/dns/zone.py”,第977行,来自from_file
filename, allow_include, check_origin)
文件“/usr/lib64/python2.6/site-packages/dns/zone.py”,第924行,in_text
reader.read()
文件“/usr/lib64/python2.6/site-packages/dns/zone.py”,第882行,阅读
self.zone.check_origin()
文件“/usr/lib64/python2.6/site-packages/dns/zone.py”,第521行,在check_origin
raise NoSOA
dns.zone.NoSOA
对于Zone文件,我从Godaddy.com下载了一个区域文件,同样的问题。我从PRODUCTION Bind服务器(辅助区域)获取了一个区域文件,错误相同。
所以我真的很难过,任何援助都会被大量拨付。
答案 0 :(得分:0)
关于脚本1:
我有类似的问题,因为SOA
未知 - 所有其他rdata类型也是如此。使用from
指令导入它们,或者提供全名,例如dns.rdatatype.SOA
,...
关于脚本2:
该演示从文件名猜测区域原点。如果文件名不是区域原点,请将os.path.basename(filename)
替换为区域的原点。