Reputation: 63
Relatively new to python and programming in general but would like to automate a DNS migration I'm working on for a large # of domains.
Shamelessly stole some initial framework from AgileTesting Blog.
In it's current state the script
I can remark out the rdata.rname = respname
in the soarr(respname)
function so I know it's something specific there. Not sure how to drill down into the issue based on the error.
For the escape characters, I feel like it's something simple but my brain is mushy so just including it as a minor problem.
#!/bin/python3
import re,sys
import dns.zone
from dns.exception import DNSException
from dns.rdataclass import *
from dns.rdatatype import *
script, filename, nameservers = sys.argv
sourcefile = open(filename,"r")
def soarr(respname):
for (name, ttl, rdata) in zone.iterate_rdatas(SOA):
serial = rdata.serial
old_name = rdata.rname
new_serial = serial + 1
print ("Changing SOA serial from %d to %d" %(serial, new_serial))
print ("Changing responsible name from %s to %s" %(old_name, respname))
rdata.serial = new_serial
rdata.rname = respname
rdata.expire = 3600
print (rdata.rname)
def nsrr(nameserver):
NS_add = "@"
target = dns.name.Name((nameserver,))
print ("Adding record of type NS:", NS_add)
rdataset = zone.find_rdataset(NS_add, rdtype=NS, create=True)
rdata = dns.rdtypes.ANY.NS.NS(IN, NS, target)
rdataset.add(rdata, ttl=3600)
print (rdata)
def savefile(domain):
print ("debug",domain)
new_zone_file = "new.%s.hosts" % domain
print ("Writing modified zone to file %s" % new_zone_file)
zone.to_file(new_zone_file,domain)
for domainitem in sourcefile:
domainitem = domainitem.rstrip()
print ("Processing %s." % domainitem)
zone_file = '%s.hosts' % domainitem
zone = dns.zone.from_file(zone_file, domainitem)
# Updating the SOA record, responsible name, lowering TTL and incrementing serial of the zone file.
soarr('systems.example.com')
# Adding name servers to the zone file.
if nameservers == 'customer':
nsrr('ns01.example.com')
if nameservers == 'core':
nsrr("ns01.example2.com")
if nameservers == 'internal':
nsrr("ns01.int.example2.com")
# Save the file as a new file.
savefile(domainitem)
The intent is to cycle through a list of domains from a file, open the appropriate zone file, manipulate the zone and save the changes to a newly named file.
Error on save failure.
Traceback (most recent call last):
File "./zonefile.py", line 62, in <module>
savefile(domainitem)
File "./zonefile.py", line 36, in savefile
zone.to_file(new_zone_file,domain)
File "/usr/local/lib/python3.6/site-packages/dns/zone.py", line 531, in to_file
relativize=relativize)
File "/usr/local/lib/python3.6/site-packages/dns/node.py", line 51, in to_text
s.write(rds.to_text(name, **kw))
File "/usr/local/lib/python3.6/site-packages/dns/rdataset.py", line 218, in to_text
**kw)))
File "/usr/local/lib/python3.6/site-packages/dns/rdtypes/ANY/SOA.py", line 62, in to_text
rname = self.rname.choose_relativity(origin, relativize)
AttributeError: 'str' object has no attribute 'choose_relativity'
As mentioned, remarking out the single line let's the file save. In the saved file the NS entries show escape characters.
@ 3600 IN NS ns01\.example\.com
Upvotes: 2
Views: 2314
Reputation: 2693
Both of the issues you've encountered - the AttributeError
and the escape characters - are because you're not creating your dns.name.Name
s correctly.
To create a dns.name.Name
from a str
, it's best to call dns.name.from_text()
.
Example: name = dns.name.from_text('example.com')
Specifically, in your nsrr
function, the second line should be changed to
target = dns.name.from_text(nameserver)
And in your soarr
function, you fix it with:
rdata.rname = dns.name.from_text(respname)
Here's a copy of the changes I made (also some minor indentation changes as well).
#!/bin/python3
import re
import sys
import dns.zone
from dns.exception import DNSException
from dns.rdataclass import *
from dns.rdatatype import *
script, filename, nameservers = sys.argv
sourcefile = open(filename,"r")
def soarr(respname):
for (name, ttl, rdata) in zone.iterate_rdatas(SOA):
serial = rdata.serial
old_name = rdata.rname
new_serial = serial + 1
print ("Changing SOA serial from %d to %d" %(serial, new_serial))
print ("Changing responsible name from %s to %s" %(old_name, respname))
rdata.serial = new_serial
rdata.rname = respname
rdata.expire = 3600
print (rdata.rname)
def nsrr(nameserver):
NS_add = "@"
target = dns.name.from_text(nameserver)
print ("Adding record of type NS:", NS_add)
rdataset = zone.find_rdataset(NS_add, rdtype=NS, create=True)
rdata = dns.rdtypes.ANY.NS.NS(IN, NS, target)
rdataset.add(rdata, ttl=3600)
print (rdata)
def savefile(domain):
print ("debug",domain)
new_zone_file = "new.%s.hosts" % domain
print ("Writing modified zone to file %s" % new_zone_file)
zone.to_file(new_zone_file,domain)
for domainitem in sourcefile:
domainitem = domainitem.rstrip()
print ("Processing %s." % domainitem)
zone_file = '%s.hosts' % domainitem
zone = dns.zone.from_file(zone_file, domainitem)
# Updating the SOA record, responsible name, lowering TTL and incrementing serial of the zone file.
soarr(dns.name.from_text('systems.example.com'))
# Adding name servers to the zone file.
if nameservers == 'customer':
nsrr('ns01.example.com')
if nameservers == 'core':
nsrr("ns01.example2.com")
if nameservers == 'internal':
nsrr("ns01.int.example2.com")
# Save the file as a new file.
savefile(domainitem)
Upvotes: 1