I was assigned a task by my boss that I have no idea how to accomplish. He needs me to build a script so that he can do various DNS admin tasks with a command line script. Although I'm not a programmer, I have at least played with python a little.
I'm not sure which would be the best way to accomplish the job. The file follows a very (at least to humans) easy 5 line pattern. The only part of each entry that changes is the domain name itself.
zone "bostire.com" {
type master;
allow-transfer {none;};
file "/etc/bind/zones/bostire.com";
};
zone "caylorscustommolds.com" {
type master;
allow-transfer {none;};
file "/etc/bind/zones/caylorscustommolds.com";
};
zone "contractorsservicesofsek.com" {
type master;
allow-transfer {none;};
file "/etc/bind/zones/contractorsservicesofsek.com";
};
I'll need to be able to have functions that can add, delete, and sort this file. For the sake of future inprovements to the script, I would like to work towards being able to do it like so:
add_entry(newdomain.tld)
del_entry(olddomain.tld)
sort_entries()
In my thinking, which I admit is not the best in the world, if I knew how to read the file into an array of some kind so that it can be sorted, I should be able to use that same array concept to delete. Adding would be straight forward, with a call to sort_entries() afterword.
I don't expect you folks to write the whole dang program for me, but if you could point me in the right direction, or throw out a few ideas for me to read about, that would be great!
I noticed that it didn't seem to be saving the sorted list. So, I set out to investigate why. Here is modified code based on Barnacle_Ed's instructions:
import re
recordslist = open('/home/bradboy/named.conf.local', 'r+')
myregex = re.compile('^zone.*^\};\n', re.DOTALL | re.MULTILINE)
mylist = myregex.findall(recordslist.read())
mylist.sort()
print len(mylist)
numitems = 0
recordslist.seek(0)
for entry in sorted(mylist):
recordslist.write(entry)
numitems += 1
print numitems
recordslist.truncate()
recordslist.close()
Which produces an output of:
bradboy@ns1:~$ python dns.py
1
1
What am I doing wrong?