如何使用python和bicop解析dhcpd.conf文件?

时间:2012-03-07 16:11:38

标签: python parsing dns dhcp

我想用python编辑文件(dhcpd.conf,dns文件)。我正在寻找任何选项,我发现了bicop库。我试着这样做:

from bicop import parse
parse("/home/tigov/dhcp/dhcpd.conf")

我得到了:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/bicop/config.py", line 83, in parse
    return _Parse(tokenizer, dictclass=dictclass)
  File "/usr/local/lib/python2.6/dist-packages/bicop/config.py", line 141, in _Parse
    raise ParseError, (input.infile, input.lineno, "Unexpected end of file")
bicop.config.ParseError: None[1]: Unexpected end of file

任何想法我必须做什么,或任何“bicop如何”?或者也许是另一个图书馆这样做? (iscpy库对我来说效果不好)感谢您的帮助和对不起,我的英语很弱。

3 个答案:

答案 0 :(得分:0)

看起来像bicop的例子不起作用。 Parse只接受字符串输入。试试这个:

from bicop import parse
parse(open("/home/tigov/dhcp/dhcpd.conf").read())

答案 1 :(得分:0)

似乎this answer在这里很有帮助。 bicop似乎不适用于解析更复杂的ISC配置文件,但iscpy似乎

# Here is the file that I'm parsing (which bicop won't parse)
#
# zone "example.com" IN {
#   type master;
#   file "/var/named/example.com";
#   allow-update { none; };
# };

>>> import iscpy
>>> iscpy.ParseISCString(open('./named.conf').read())
{'zone "example.com" IN': {'allow-update': {'none': True},
  'file': '"/var/named/example.com"',
  'type': 'master'}}

答案 2 :(得分:0)

如果您想以一种漂亮的方式修改ISC文件,保留注释,适应现有格式等,confctl实用程序可能是一个不错的选择:https://github.com/trasz/confctl/