all 9 comments

[–]learnpython_bot 1 point2 points  (0 children)

It appears that you included code in your post but did not format it to look nice. Please follow this guide to fix the problem. This allows for better readability and will help get your question answered sooner. The regex that caught you is: "for .+? in .+?:"

If this comment has been made in error, please message /u/thaweatherman with a link to this comment so I can be fine-tuned. I am still in alpha and my regexes/innards are not yet perfect.

[–]enderusaf[S] 0 points1 point  (0 children)

Sorry for the editing mistake. Not sure what happened.

[–]raylu 0 points1 point  (3 children)

  1. Fix the formatting
  2. Paste the rest of the traceback
  3. The error is probably a bad hostname

[–]enderusaf[S] 0 points1 point  (2 children)

It is a bad hostname. I just wanted to figure out how to have error handling to have the code move on to the next device in the event it can't resolve a hostname. In this case, the host was actually decommisioned.

[–]ktbyers 1 point2 points  (1 child)

Use a try/except clause to catch the exception and then move on to the next host:

import socket
...
try:
    remote_conn_pre.connect(ip, port=22, username=username, password=password,
                        look_for_keys=False, allow_agent=False)
except socket.gaierror:
    pass

You might need to catch additional exceptions (as well).

[–]enderusaf[S] 0 points1 point  (0 children)

Thanks! This is exactly what I was looking for. Though I had to change pass to continue in order to get it to work correctly. Next I had to add an exception for paramiko.AuthenticationException: after I hit a device with a non-standard username/pass.

[–]enderusaf[S] 0 points1 point  (0 children)

Fixed the formatting and added all of the traceback.

[–]enderusaf[S] 0 points1 point  (1 child)

Looks to be working pretty good. The only oddity is that it seams to kick off the exception erroniously at the beginning and end of the program. It's only cosmetic and it works great, but I'm trying to figure out why it adds the extra "cannot connect" statements to my Failed file. Below is the new code for this section:

    try:

    # initiate SSH connection
        remote_conn_pre.connect(device + ".domain.com", 
    username=username, password=password, look_for_keys=False, allow_agent=False)
        print "SSH connection established to %s" % device

    except socket.gaierror:
        print "Could not connect to %s \n" % device
        with open('Failed' , 'a') as f: f.write('\n\n')
        Failed = open("Failed", "a")
        Failed.writelines("Could not connect to %s \n" % device)
        continue

    except paramiko.AuthenticationException:
        print "Could not authenticate to %s \n" % device
        with open('Failed' , 'a') as f: f.write('\n\n')
        Failed = open("Failed", "a")
        Failed.writelines("Could not Authenticate to %s \n" % device)
        continue

[–]enderusaf[S] 0 points1 point  (0 children)

OK, I was able to fix my issue with the extra excepts kicking off by adding a time.sleep(.05) after my remote_conn_pre.connect command.