简体   繁体   中英

Reading a string as a config value using ConfigParser

I'm trying to write a utility that will let me set commands to run in a unix shell, based off config files. Essentially, I want to be able to give a [Command List] and an expected [Response List] .. Here's an example of one of my config files:

# Command File
# List of commands to be carried out for each host listed in `veri.serv.cfg`

[Command List]
; T server
nc -zw1 159.1.1.1 9988 | gawk '{print $7}'

; P Server
nc -zw1 sup.serv.com 8050 | gawk '{print $7}'

; G L Service
nc -zw1 hi.serv.com 80 | gawk '{print $7}'

; L and N Hosts
nc -zw1 l.serv.com 80 | gawk '{print $7}'
nc -zw1 ln.serv.com 443 | gawk '{print $7}'
nc -zw1 llnn.serv.com 443 | gawk '{print $7}'

However, when I attempt to parse it using parser.items('Command List') , I get:

  File "veri.py", line 22, in <module>
    print subConfigParser.read(os.path.join(relativeRunPath, 'veri.cfg'))
  File "/usr/lib/python2.7/ConfigParser.py", line 305, in read
    self._read(fp, filename)
  File "/usr/lib/python2.7/ConfigParser.py", line 546, in _read
    raise e
ConfigParser.ParsingError: File contains parsing errors

[line  6]: "nc -zw1 159.1.1.1 9988 | gawk '{print $7}'\n"
[line  9]: "nc -zw1 sup.serv.com 8050 | gawk '{print $7}'\n"
[line 12]: "nc -zw1 hi.serv.com 80 | gawk '{print $7}'\n"
[line 15]: "nc -zw1 l.serv.com 80 | gawk '{print $7}'\n"
[line 16]: "nc -zw1 ln.serv.com 443 | gawk '{print $7}'\n"
[line 17]: "nc -zw1 llnn.serv.com 443 | gawk '{print $7}'\n"

Can I not read in full strings as a value using a SafeConfigParser ?

Full File:

#! /usr/bin/python25
import os
import ConfigParser

# --- sudo consts --- #
# relative path, regardless of where we're executing from
RELATIVE_PATH = os.path.abspath(os.path.dirname(__file__))
BASE_SERVER_FILE = os.path.join(RELATIVE_PATH, 'cfg', 'veri.serv.cfg')
BASE_CONFIG_FILE = os.path.join(RELATIVE_PATH, 'cfg', 'veri.base.cfg')
# --- end consts --- #

baseConfigParser = ConfigParser.SafeConfigParser()
baseConfigParser.read(BASE_CONFIG_FILE)
runlist = baseConfigParser.get('Config List', 'runlist').strip().split("\n")
print runlist

for subDir in runlist:
    print subDir
    relativeRunPath = os.path.join(RELATIVE_PATH, subDir)
    subConfigParser = ConfigParser.SafeConfigParser()
    subConfigParser.read(os.path.join(relativeRunPath, 'veri.cfg'))
    print subConfigParser.items('Command List')            ###### <<< Error Here
    if os.path.isfile(os.path.join(relativeRunPath, 'veri.serv.cfg')):
        subServSpecified = true
        subServConfigParser = ConfigParser.SafeConfigParser()
        subServConfigParser.read(os.path.join(relativeRunPath, 'veri.serv.cfg'))
        print subServConfigParser.get('Config List', 'runlist')

Your config file doesn't use a valid format for ConfigParser. It needs to be a series of sections followed by named options, such as

[T server]
commands = nc -zw1 159.1.1.1 9988 | gawk '{print $7}'

[P Server]
commands = nc -zw1 sup.serv.com 8050 | gawk '{print $7}'

[G L Service]
commands = nc -zw1 hi.serv.com 80 | gawk '{print $7}'

[L and N Hosts]
commands = nc -zw1 l.serv.com 80 | gawk '{print $7}'
    nc -zw1 ln.serv.com 443 | gawk '{print $7}'
    nc -zw1 llnn.serv.com 443 | gawk '{print $7}'

You can then get the values

>>> from ConfigParser import SafeConfigParser
>>> parser = SafeConfigParser()
>>> parser.read('myconfig.ini')
['myconfig.ini']
>>> for section in parser.sections():
...     print 'section', section
...     for option in parser.options(section):
...             print parser.get(section, option)
... 
section T server
nc -zw1 159.1.1.1 9988 | gawk '{print $7}'
section P Server
nc -zw1 sup.serv.com 8050 | gawk '{print $7}'                                                                                                                                  
section G L Service                                                                                                                                                            
nc -zw1 hi.serv.com 80 | gawk '{print $7}'                                                                                                                                     
section L and N Hosts                                                                                                                                                          
nc -zw1 l.serv.com 80 | gawk '{print $7}'                                                                                                                                      
nc -zw1 ln.serv.com 443 | gawk '{print $7}'                                                                                                                                    
nc -zw1 llnn.serv.com 443 | gawk '{print $7}'                                                                                                                                  
>>>                                                                                 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM