Any python libs for parsing Bind zone files?

前端 未结 5 745
鱼传尺愫
鱼传尺愫 2020-12-06 02:54

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 fi

相关标签:
5条回答
  • 2020-12-06 03:02

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

    0 讨论(0)
  • 2020-12-06 03:11

    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

    0 讨论(0)
  • 2020-12-06 03:11

    easyzone is a nice layer over dnspython

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

    0 讨论(0)
  • 2020-12-06 03:12

    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.

    0 讨论(0)
  • 2020-12-06 03:13

    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.

    0 讨论(0)
提交回复
热议问题