Question

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

Was it helpful?

Solution

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

OTHER TIPS

easyzone is a nice layer over dnspython

Zoner provides a web-interface for editing zone files and makes use of easyzone.

I know this is old but the only working one I could find is called iscpy. You can do an easy_install.

easy_install iscpy

Then in python:

import iscpy
iscpy.ParseISCString(open('somefile.conf', 'r').read())

Which returns a dictionary.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top