Pergunta

I am using paramiko to login to a set of servers and just check if my credentials work.I do not need to run any further commands.I know the exceptions raised for authentication failure and connection error.However,I am not sure of what is returned on a 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 do I substitute the "IF CONDITION MET THEN" with ?

Foi útil?

Solução

You dont need a IF CONDITION. Because if authentication fails, script will anyway raise exception and will not execute rest of the statements

So you should just put the statements after ssh.connect, as they will only be executed if the connection is successful.

If you still want to put IF CONDITION what you can do is

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

if conn is None:
         print "Successfully Authenticated"
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top