使用python iscpy模块

时间:2014-08-31 20:25:48

标签: python python-2.7

我想加载各种配置文件,更改一些设置并再次将其写回。搜索后,听起来iscpy模块可能有用。麻烦的是我无法弄清楚如何使用它,我无法在网上找到任何例子。有没有人用它,如果可以,你可以给我一些样品吗?

1 个答案:

答案 0 :(得分:3)

这是一个程序,它读取ISC样式的配置文件,修改配置,并写入新的配置文件:

import iscpy

# Read in an existing config file
with open('/tmp/named.conf') as input_config_file:
    config_string = input_config_file.read()
config_dict = iscpy.ParseISCString(config_string)

# Modify the configuration
config_dict['zone "example.com"'] = {
    'file':'"zone/example.com"',
    'type':'master'
}

# Write out the new config
config_string = iscpy.MakeISC(config_dict)
with open('/tmp/named-new.conf', 'w') as output_config_file:
    output_config_file.write(config_string)

请注意,此转换会保留原始内容中可能包含的任何注释或空格。

示例输入:

// Boot file for LAND-5 name server

options {
        directory "/var/named";
};

controls {
        inet 127.0.0.1 allow { localhost; } keys { rndc_key; };
};

key "rndc_key" {
        algorithm hmac-md5;
        secret "c3Ryb25nIGVub3VnaCBmb3IgYSBtYW4gYnV0IG1hZGUgZm9yIGEgd29tYW4K";
};

zone "." {
        type hint;
        file "root.hints";
};

zone "0.0.127.in-addr.arpa" {
        type master;
        file "zone/127.0.0";
};

zone "land-5.com" {
        type master;
        file "zone/land-5.com";
};

zone "177.6.206.in-addr.arpa" {
        type master;
        file "zone/206.6.177";
};

示例输出:

zone "177.6.206.in-addr.arpa" { type master;
file "zone/206.6.177"; };
key "rndc_key" { secret "c3Ryb25nIGVub3VnaCBmb3IgYSBtYW4gYnV0IG1hZGUgZm9yIGEgd29tYW4K";
algorithm hmac-md5; };
controls { inet 127.0.0.1 allow { localhost; } keys { rndc_key; }; };
zone "example.com" { type master;
file "zone/example.com"; };
zone "." { type hint;
file "root.hints"; };
zone "0.0.127.in-addr.arpa" { type master;
file "zone/127.0.0"; };
zone "land-5.com" { type master;
file "zone/land-5.com"; };
options { directory "/var/named"; };