Paramiko fails to connect to SSH server - says Authentication Failed though username and password is given correct

Paramiko version - 2.7.2

Python version - 3.5.10

Operating System - Linux, CentOS Linux 7 (Core)

**Problem : ** Using python, we are trying to connect to a SSH server. It randomly fails with "authentication failed" error. Whereas it perfectly works sometimes. When we try to SSH to the server from command line it connects fine.

Note : Hostname, Username and password given are correct.

Below is the code snippet and traceback -
**Steps : **
1.) Running below code via crontab with 15mins interval.
2.) Seeing authentication failed error randomly.

import paramiko  
import traceback
 
ssh = paramiko.SSHClient()  
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  
try:  
    ssh.connect(hostname, 22, username, password)  
    ssh.close()
except Exception as ssherror:  
    traceback.print_exc()
Traceback (most recent call last):  
File "sshToServer.py", line 15, in getCPUCount  
ssh.connect(hostname, 22, username, password)  
File "/home/user/env/lib/python3.5/site-packages/paramiko/client.py", line 446, in connect  
passphrase,  
File "/home/user/env/lib/python3.5/site-packages/paramiko/client.py", line 764, in \_auth  
raise saved_exception  
File "/home/user/env/lib/python3.5/site-packages/paramiko/client.py", line 751, in \_auth  
self.\_transport.auth_password(username, password)  
File "/home/user/env/lib/python3.5/site-packages/paramiko/transport.py", line 1509, in auth_password  
return self.auth_handler.wait_for_response(my_event)  
File "/home/user/env/lib/python3.5/site-packages/paramiko/auth_handler.py", line 250, in     wait_for_response  
raise e  
paramiko.ssh_exception.AuthenticationException: Authentication failed.

Can you please help us to understand what is causing the issue?

Expectation : Need to connect to SSH server without failure.

Upvotes: -1

Views: 76

Answers (1)

Mako212
Mako212

Reputation: 7312

To the point that @Lewis makes, your try/except block doesn't guarantee that the connection gets cleaned up properly, especially if you're running additional instructions after ssh.connect() in the try block.

I'd suggest starting with this, moving ssh.close() to the finally block which should guarantee that the SSH session is closed properly

try:  
    ssh.connect(hostname, 22, username, password)  
except Exception as ssherror:  
    traceback.print_exc()
finally:
    ssh.close()

try/except/finally works, but I like to define a context manager for this situation so I can fully the handle the connection within a with statement.

class SSHConnection:
    def __init__(self, hostname, port, username, password):
        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.hostname = hostname
        self.port = port
        self.username = username
        self.password = password

    def __enter__(self):
        self.ssh.connect(self.hostname, self.port, self.username, self.password)
        return self.ssh

    def __exit__(self, exc_type, exc_value, traceback):
        self.ssh.close()

That way I can just call it as:

with SSHConnection(hostname, port, username, password) as ssh:
    ssh.exec_command()

Upvotes: 0

Related Questions