Reputation: 522
My idea is to fetch all the component versions installed in all the different labs across the world. My code works when i give the details explicitly.
My code is as 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' )
with 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')
with open("UK_PDL.txt", "w") as fo:
fo.write(files)
components = []
with open("UK_PDL.txt", 'rb') as fo:
strings = ("/installed/")
i=0
for 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 is as 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 i have written functions for each IP. Instead of this how can i make sure that all the entries are read from the config one by one and fetch the details?
Upvotes: 0
Views: 4211
Reputation: 350310
You could loop over the sections and build file names dynamically:
def any_HE():
global config
config = ConfigParser.RawConfigParser()
config.read('config.cfg')
for section in config.sections():
list = {} #start with empty list for each section
env.user = config.get(section, 'db.user_name')
env.password = config.get(section, 'db.password')
host = config.get(section, 'db.ip')
with 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')
with open(section + ".txt", "w") as fo:
fo.write(files)
components = []
with open(section + ".txt", 'rb') as fo:
strings = ("/installed/")
i=0
for 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 your code seemed to be wrong at some places, so I had to make some assumptions.
Upvotes: 2
Reputation: 7850
To loop over the sections in a config file:
config = ConfigParser()
config.read('config.cfg')
for section in config.sections():
print(section, dict(config[section]))
To access the n
th section:
config[config.sections()[n]]
Upvotes: 1