Paramiko Error Handling (Python)

Asked

Viewed 388 times

7

Hello,

I am mounting a script where the user will authenticate on a device, if everything goes well the code continues otherwise it shows an error message. I am using the Paramiko library to do the session with the equipment, the connection is ok and it returns the message I want when the connection fails, but another error appears soon after.

MY CODE:

username = raw_input("Username:")
password = getpass.getpass("Password: ")

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

try:
    remote_conn_pre.connect('1.111.111.111', username=username, password=password)
except paramiko.ssh_exception.AuthenticationException:
    print ("Erro de login.")

ERROR NEXT:

  File "run.py", line 67, in <module>
    remote_conn_pre.connect(ip, username=username, password=password)
  File "C:\Python27\lib\site-packages\paramiko\client.py", line 380, in connect
    look_for_keys, gss_auth, gss_kex, gss_deleg_creds, gss_host)
  File "C:\Python27\lib\site-packages\paramiko\client.py", line 603, in _auth
    raise saved_exception
paramiko.ssh_exception.AuthenticationException: Authentication failed.

I don’t think I know how to use the paramiko.ssh_exception.AuthenticationException right, someone can help me?

And one more question, There is how I do if it gives error, it return to the user to enter the username/ password again?

I’m sorry, I’m kind of new to programming and Python, so I was wondering. Thank you very much!

  • 1

    already tried only except without the paramiko.ssh_exception.AuthenticationException?

1 answer

1

You’re not treating the exception correctly. On the block try except add the process of authentication exception

try:
    remote_conn_pre.connect('1.111.111.111', username=username, password=password)
except AuthenticationException:
    print ("Erro de login.")

or

try:
    remote_conn_pre.connect('1.111.111.111', username=username, password=password)
except paramiko.AuthenticationException as err:
    print ("Erro de login: " + str(err)))

Read the library documentation on the method connect of client he of raise in the exceptions of BadHostKeyException , AuthenticationException, SSHException and socket.error

http://docs.paramiko.org/en/stable/api/client.htm

Browser other questions tagged

You are not signed in. Login or sign up in order to post.