Any python libraries for parsing bind zone files?

Any python libraries for parsing bind zone files? Basically something that will help in adding / removing zones and records. This should work even if someone modifies the zone file manually, so overwriting zone files every time is not a solution.

+6
python bind
source share
5 answers

I was unable to use bicop for classic zone files, such as:

$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 look dnspython

+5
source share

easyzone - nice layer on top of dnspython

Zoner provides a web interface for editing zone files and uses easyzone.

+6
source share

I know this is old, but the only worker I could find is called iscpy. You can do easy_install.

 easy_install iscpy 

Then in python:

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

Which returns a dictionary.

+1
source share

See the answer above about bicop.

Aside, the Python package index at http://pypi.python.org/pypi is a great place to search for Python packages.

EDIT . It may be useful below if someone is trying to figure out a simple parsing, but bicop seems to be an existing solution.

If someone changed the configuration manually, and you do not want to overwrite it, does this mean that you want to insert / remove lines from the existing configuration, leaving all comments, etc. intact? This prevents parsing and then re-displays the configuration, but it’s also positive - you don’t need to parse the file completely to achieve your goal.

To add an entry, you can 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 to delete 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 can get as much as you need with simple code snippets like this.

0
source share

You can try bicop , "the python library for processing ISC style configuration files."

0
source share

All Articles