tags:

views:

647

answers:

4

Any python libs for parsing Bind config files? Basically something that will aid in adding/removing zones and records. This needs to work even if someone modifies the config by hand so overwriting the configs every time is not a solution.

+1  A: 

See answer above about bicop.

As an aside, the Python Package Index at http://pypi.python.org/pypi is a great place to look for Python packages.

EDIT: The below may still be helpful to someone trying to figure out simple parsing, but bicop is apparently an existing solution.

If someone has modified the config by hand, and you don't want to overwrite it, does that imply that you wish to insert/remove lines from an existing config, leaving all comments etc intact? That does prevent parsing then re-outputting the config, but that's a positive as well -- you don't need to fully parse the file to accomplish your goal.

To add a record, you might try a simple approach like

# define zone_you_care_about and line_you_wish_to_insert first, then:
for line in bindfile.read():
    out.write(line + '\n')
    if ('zone "%s" in' % zone_you_care_about) in line:
        out.write(line_you_wish_to_insert)

Similar code works for removing a line:

# define zone_you_care_about and relevant_text_to_remove, then:
for line in bindfile.read():
    if not relevant_text_to_remove in line:
        out.write(line + '\n')

You may get as far as you need with simple snippets of code like this.

Michael Gundlach
See my answer below about bicop. Perhaps you should have used google (it's the second hit for "bind config file in python") rather than pypi's deficient search interface :).
Glyph
I did google it and managed to miss it nonetheless. I think I tried "bind python parse". I'll update my response.
Michael Gundlach
+2  A: 

You might try bicop, "a python library to process ISC bind-style configuration files".

Glyph
Does this support daniels's requirement that by-hand modifications be retained?
Michael Gundlach
A: 

take a look at webmin

ping
+1  A: 

I was unable to use bicop for classical zone files like these:

    $TTL 86400
@   IN SOA ns1.first-ns.de. postmaster.robot.first-ns.de. (
    2006040800   ; serial
    14400        ; refresh
    1800         ; retry
    604800       ; expire
    86400 )      ; minimum

@

                    IN NS      ns1.first-ns.de.

I will have a look at dnspython

NorbertK