2014-08-31 91 views
0

我想加載各種配置文件,更改一些設置並將它們重新寫回。搜索後,聽起來像iscpy模塊可能會有用。麻煩是我不知道如何使用它,我無法在網上找到任何例子。有沒有人使用它,如果是的話,你可以給我一些樣品嗎?使用python iscpy模塊

+0

爲什麼不能民間有膽量說出個所以然downvote /要求它關閉? – spiderplant0 2014-08-31 20:37:38

+0

我會努力達到平衡!聽起來像一個有趣的模塊,但它記錄不完整,我也找不到很多信息。我認爲它是一個合法的問題。 – tdelaney 2014-08-31 20:42:04

+0

謝謝@tdelaney。關閉警察:關閉被標記爲「脫離主題」。但是,在我看到的準則中,它確實解決了一個特定的編程問題(特別是如何使用'iscpy'),它是可以回答的,並且不會導致'自以爲是的答案'。但最重要的是,一些簡單的使用示例對許多程序員都有用。 – spiderplant0 2014-08-31 21:04:46

回答

2

這裏有一個程序,它讀取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"; };