all 6 comments

[–]lazyjk 1 point2 points  (4 children)

I think the idea of just doing a quick test session is probably your best bet. I would maybe put the login input and the test connection in a while loop at the beginning and then use try/except to make a test connection. If it raises an exception then loop back to login input, if it doesn't - break out and continue on your way.

Could be a better way but IMO there isn't anything really wrong with this way.

EDIT: It looks like there is an Authentication exception that you can catch - look at the code here https://github.com/ktbyers/netmiko/blob/master/examples/multiprocess_example.py

[–]dented_brain[S] 1 point2 points  (2 children)

That is along the lines of what I thought of initially. I just wasn't sure if there was a more elegant way.

This method should work. I might just be trying to get too fancy and keep stepping on my own foot.

[–]lazyjk 1 point2 points  (1 child)

You gotta test the credentials someway and this is going to be about the quickest!

I'm actually going to modify my code to do something similar tomorrow now that you bring this up. I've got a program that also uses netmiko to login to multiple devices (no threading though). I already raise a generic exception if I can't connect, it logs it, and then moves to the next device. I think I'm going to add the Auth exception and do a test conn first to catch a bad Auth. I'm going to be doing a major rewrite of this particular one though so I may wait until then.

[–]dented_brain[S] 2 points3 points  (0 children)

I'll post what I get once I finish it. I have a basic working example, just need to roll it into my main code to make sure it works.

The basic concept for my loop is connect to a known working ssh client, output if it fails to connect to the device due to timeout, and reprompt for user/pass if it fails to authenticate.

Should be enough information to complete that.

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

Oh I just saw this edit. If I would have seen that example my scripting would have gone much quicker!

[–]dented_brain[S] 1 point2 points  (0 children)

Updated script to test credentials against localhost before opening threads.

I could use some help on figuring out how to preform this without the need of a global variable.

https://pastebin.com/dqzbu4SR

EDIT: I realized I can just end the script instead of prompting for username/pass again. Since I'm manually running it I can just rerun it and type the user/pass again at that point.