ConfigParser模块进行
代码如下 | 复制代码 |
#!/usr/bin/python import ConfigParser cf = ConfigParser.ConfigParser() if __name__=="__main__" : cf.read('test.conf') tcpdumpNet = cf.get("tcpdump_param", "tcpdump_netdevice") tcpdumpPro = cf.get("tcpdump_param", "tcpdump_protocol") sshIP = cf.get("sshParam", "remove_hostIP") sshPort = cf.get("sshParam", "remove_hostPort") interval = cf.get("otherparam", "interval") under_value = cf.get("otherparam", "under_value") print tcpdumpNet,tcpdumpPro,sshIP,sshPort,interval,under_value ~ |
结果
代码如下 | 复制代码 |
./configParser.py eth1 tcp 192.168.1.100 22 3 7 |
有时候配置文件需要和shell程序共享
代码如下 | 复制代码 |
#!/usr/bin/python import re configfile=file('test.conf','r') for line in configfile: list=re.split('s+|=+',line) param=list[0] value=list[1] if (param=='tcpdump_protocol'): tcpdumpPro=value elif param == 'remove_hostIP': sshIP=value elif param == "tcpdump_netdevice": tcpdumpNet=value elif param == "remove_hostPort": sshPort=value elif param == "interval": interval=value elif param == "under_value": under_value=value print tcpdumpNet,tcpdumpPro,sshIP,sshPort,interval,under_value |
结果
代码如下 | 复制代码 |
./readConfigFile.py eth1 tcp 192.168.1.100 22 3 7 |
时间: 2024-09-24 19:15:27