2

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: 
Roshan r
  • 522
  • 2
  • 11
  • 30
  • Could you be more specific on what the "run" command does? Is this the command that hangs? – alpha1554 Feb 10 '16 at 09:57
  • Alpha..When i run the script, it hangs there. The output, i have showed above. – Roshan r Feb 10 '16 at 10:12
  • If you want to run shell commands (like ```ls -ltr /opt/nds```) with a timeout in python 2.7, I suggest you take a look at these answers : http://stackoverflow.com/questions/1556348/python-run-a-process-with-timeout-and-capture-stdout-stderr-and-exit-status . In python 3, the ```subprocess``` module natively supports a timeout option. – alpha1554 Feb 10 '16 at 10:16
  • Actually, i didnt post the full script since its big. My idea is to run the shell command and save it to text file. Then process the text file. My script works perfectly fine if all the details in the config are correct. I wanted to check for negative cases with wrong IP and network not reachable. So, how will i check for exceptions here. – Roshan r Feb 10 '16 at 10:24
  • The problem is that the exception seems to occur within the shell command. This means python doesn't 'see' the exception. Also, I don't think there is an actual exception, the program just hangs. In order to avoid hanging, you need a timeout. – alpha1554 Feb 10 '16 at 10:28
  • Is there a way where i can check for exception before the the ls -ltr /opt/nds? So that if network connection was done, i can exit from that – Roshan r Feb 10 '16 at 10:30
  • That would probably require you to establish a TCP connection beforehand. See https://docs.python.org/3/howto/sockets.html for sockets. – alpha1554 Feb 10 '16 at 10:32

0 Answers0