python - Reading all sections in a config file -
my idea fetch component versions installed in different labs across world. code works when give details explicitly.
my code follows
def uk_pdl_he(): uk_pdl_list = {} sorted_list = {} slist = {} he_string = "uk_pdl_he" global config config = configparser.rawconfigparser() print config.sections() config.read('config.cfg') env.user = config.get('uk_pdl','db.user_name' ) env.password = config.get('uk_pdl','db.password' ) host = config.get('uk_pdl','db.ip' ) settings(hide('warnings', 'running', 'stdout', 'stderr'), warn_only=true, host_string=host): paramiko.util.log_to_file('uk_pdl.log') files = run('ls -ltr /opt/nds') open("uk_pdl.txt", "w") fo: fo.write(files) components = [] open("uk_pdl.txt", 'rb') fo: strings = ("/installed/") i=0 line in fo: if strings in line: id = re.search('installed/(.+)',line) if id: components.append(id.group(1)) component,version = components[i].rstrip().split('-',1) uk_pdl_list[component] = version i+=1 write_data(uk_pdl_list, he_string,1)
the config file follows
[uk_pdl] db.user_name = user db.password = password db.ip = 101.815.117.193 [uk_dth] db.user_name = user db.password = password db.ip = 272.119.411.121
currently have written functions each ip. instead of how can make sure entries read config 1 one , fetch details?
you loop on sections , build file names dynamically:
def any_he(): global config config = configparser.rawconfigparser() config.read('config.cfg') section in config.sections(): list = {} #start empty list each section env.user = config.get(section, 'db.user_name') env.password = config.get(section, 'db.password') host = config.get(section, 'db.ip') settings(hide('warnings', 'running', 'stdout', 'stderr'), \ warn_only=true, host_string=host): paramiko.util.log_to_file(section + '.log') files = run('ls -ltr /opt/nds') open(section + ".txt", "w") fo: fo.write(files) components = [] open(section + ".txt", 'rb') fo: strings = ("/installed/") i=0 line in fo: if strings in line: id = re.search('installed/(.+)',line) if id: components.append(id.group(1)) component,version = components[i].rstrip().split('-',1) list[component] = version i+=1 write_data(list, section + "_he", 1)
the indenting of code seemed wrong @ places, had make assumptions.
Comments
Post a Comment