I am writing a script where the script reads lot of sections [Each section containing the IP, username and password]. I am facing an issue with exceptions here. In case, if connection with any of the server is not possible, i wanted to print the correct exception and move to the next section. Currently if the network is not reachable, it stays there. Can someone help me to get the correct exception here?
Code:
def any_HE():
global config, logger
config = ConfigParser.RawConfigParser()
config.read('config.cfg')
for section in config.sections():
components = dict() #start with empty dictionary for each section
if not config.has_option(section, 'server.user_name'):
continue
env.user = config.get(section, 'server.user_name')
env.password = config.get(section, 'server.password')
host = config.get(section, 'server.ip')
try:
with settings(hide('warnings', 'running', 'stdout', 'stderr'),warn_only=True, host_string=host):
files = run('ls -ltr /opt/nds')
except Exception, e:
print e
Config file is like this:
[Logger]
#Possible values for logging are INFO, DEBUG and ERROR
log_level = DEBUG
[astro1a]
server.user_name = root
server.password = stains
server.ip = 172.19.40.21
[astro1b]
server.user_name = root
server.password = stains
server.ip = 172.19.41.22
Here, i have given the IP's wrong for testing and the output is as follows. The program will never exit.
Output:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/roradhak/Work/Scripts/ComponentListing/testLogging.py
Timed out trying to connect to 172.19.40.21 (tried 1 time)
No hosts found. Please specify (single) host string for connection: