[Tutor] Writing bind9 zone files with python

Toni Fuente tonifuente at yahoo.co.uk
Wed Aug 7 15:10:18 CEST 2013


Hi again,

I've got a Berkeley database with a key = 'domain name' and a value =
'timestamp'. And then I've got a list with items that I've got from a
LDAP search, it is a list of tuples. Each tuple is formed of a string,
which is a LDAP dn, and a dictionary with two key-value pairs,
dnszonename: ['bladibla.com'] a domain name, and modifytimestamp:
['20130723201557Z'], with a number representing the last time that the
domain was modified.

This is a data example stored in a list variable from the LDAP search
(ldapEntries):

[('cn=example1.com,ou=dns,o=mycompany.com',
 {'dnszonename': ['example1.com'],
     'modifytimestamp': ['20120601100135Z']}),

[...]

('cn=exampleN.com,ou=dns,o=mycompany.com',
 {'dnszonename': ['exampleN.com'],
    'modifytimestamp': ['20130801100135Z']})]

I need to write something in python that would go through all the
domains in the ldap search and if the domain exist on the local
database, it then compares the timestamps, if they are the same, nothing
will be done, but if they are different then, it will need to do another
LDAP search for the zone, and then write a bind zone file in the
appropriate place, and update the timestamp value in the local database
with the one from LDAP. Finally if the domain zone name doesn't exist on
the local database, it will need to write a bind zone file in the
appropriate place and add a new key-value from the domain-timestamp on
the local database.

This is a sketch of what I am doing:

ldapDict = {}

ldapEntries = ld.search_s(ldapBase,ldap.SCOPE_SUBTREE,'(&(objectclass=dnszone)(cn=*))',['dnszonename','modifytimestamp'])

for entry in ldapEntries:
    tmpDict = entry[1]
    ldapDict[''.join(tmpDict['dnszonename'])] = ''.join(tmpDict['modifytimestamp'])

for k, v in ldapDict.iteritems():
    value = zonesdb.get(k)
    if value == None:
        print "New domain"
    elif v != zonesdb[k]:
        print "New record"

The first for loop converts the LDAP search list, into a dictionary of
key-values that I need to work with. And the second loop depending of
the values or existence of the keys, It will do something or the other.

My two questions are:

- Is this the right approach to solve the problem? Is there better ones?

- I don't know how to approach the problem of writing bind zone files
  with the data that I will get from the LDAP server, so I was wondering
  if anyone knows a module that will help me.
  I am looking at things on the Internet like bind2csv.py, but it looks
  a daunting task for someone with my python skills level, to reverse
  that script. 

Thank you,
-- 
Toni

El que se casa fuera, o la trae o la lleva. 


More information about the Tutor mailing list