任何用于解析绑定区域文件的 python 库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/236859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:42:21  来源:igfitidea点击:

Any python libs for parsing Bind zone files?

pythonbind

提问by daniels

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.

任何用于解析绑定区域文件的 python 库?基本上可以帮助添加/删除区域和记录的东西。即使有人手动修改区域文件,这也需要工作,因此每次都覆盖区域文件不是解决方案。

采纳答案by NorbertK

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

我无法将 bicop 用于像这样的经典区域文件:

    $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

我会看看dnspython

回答by Boden Garman

easyzoneis a nice layer over dnspython

easyzonednspython上的一个很好的层

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

Zoner提供了一个用于编辑区域文件的网络界面,并使用了 easyzone。

回答by jccnu619

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

我知道这很旧,但我能找到的唯一有效的方法是 iscpy。你可以做一个easy_install。

easy_install iscpy

Then in python:

然后在python中:

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

Which returns a dictionary.

它返回一个字典。

回答by Michael Gundlach

See answer above about bicop.

请参阅上面关于 bicop 的答案。

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

顺便说一句,http://pypi.python.org/pypi 上的 Python 包索引是查找 Python 包的好地方。

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

编辑:下面的内容可能对试图找出简单解析的人仍然有帮助,但 bicop 显然是一个现有的解决方案。

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.

您可以使用像这样的简单代码片段来达到您的需要。

回答by Glyph

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

您可以尝试bicop“处理 ISC 绑定样式配置文件的 Python 库”。