使用python(dhcpd.conf)解析ISC文件 - iscpy模块

时间:2013-10-06 18:14:20

标签: python parsing dhcp

解析dhcpd.conf文件的最佳方法是什么?

ddns-update-style none;
authoritative;
option domain-name "example.org"
option domain-name-servers ns1.example.org, ns2.example.org


subnet 172.16.31.0 netmask 255.255.255.0 {
    # default gateway
    option routers 172.16.31.10;
    option subnet-mask 255.255.255.0;

    option domain-name "aaaaaa";
    option domain-name-servers 172.16.31.10;
    #option nis-domain "domain.org";

    range dynamic-bootp 172.16.31.80 172.16.31.90;
    default-lease-time 21600;
    max-lease-time 43200;

    host test {
        hardware ethernet 00:23:8b:42:3f:d1;
        fixed-address 172.16.31.3;
    }

}

我试过iscpy module:

a = iscpy.ParseISCString(open('dhcpd.conf', 'r').read())

该模块使用option作为键和字符串旁边的字符串作为dict的值。但是如果选项看起来不合适,那就不好了。

option domain-name "example.org"
option domain-name-servers ns1.example.org, ns2.example.org

应该是:

{'option domain-name':'example.org', 'option domain-name-servers":'ns1.example.org, ns2.example.org'}

但输出是:

{'option':'domain-name-servers: ns1.example.org, ns2.example.org'}

是用这个或其他模块做得更好的方法吗?感谢

1 个答案:

答案 0 :(得分:0)

我建议完全阅读dhcpd.conf“,然后根据每个指令制作一个data structure,例如dict或类,类是优先的,然后你可以解析你的数据。

相关问题