Paramiko return code on successful login

I use paramiko to log in to a set of servers and just check to see if my credentials work. I do not need to run any additional commands. I know the exceptions caused by the authentication error and the connection error. However, I am not sure what is returned upon successful login. This is what I have so far:

f1 = open('hostfile','r')
devices = f1.readlines()

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())


for device in devices:
    try:
            ssh.connect(device, username=username, password=password, timeout=4)
            <IF CONDITION MET THEN>
            output= "Successfully Authenticated"
            ssh.close()
    except  paramiko.AuthenticationException:
            output = "Authentication Failed"
            print output
    except  socket.error, e:
            output = "Connection Error"
            print output

What can I replace "IF CONDITION MET THEN" with?

+4
source share
1 answer

You do not need EU CONDITION. Because if authentication fails, the script will throw an exception in any case and will not execute the rest of the instructions

, ssh.connect, , .

IF CONDITION, ,

conn = ssh.connect(device, username=username, password=password, timeout=4)

if conn is None:
         print "Successfully Authenticated"
+4

Source: https://habr.com/ru/post/1526726/


All Articles