Configparser module is
The code is as follows |
Copy Code |
#!/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 ~
|
Results
The code is as follows |
Copy Code |
./configparser.py Eth1 TCP 192.168.1.100 22 3 7 |
Sometimes configuration files need to be shared with shell programs
The code is as follows |
Copy Code |
#!/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 |
Results
The code is as follows |
Copy Code |
./readconfigfile.py Eth1 TCP 192.168.1.100 22 3 7 |