Repository navigation
Description
Are you using paramiko as a client or server?
Client
What feature(s) aren't working right?
SFTP, Keys/auth
What version(s) of paramiko are you using?
3.2.0
What version(s) of Python are you using?
3.7.v27
What operating system and version are you using?
not sure, running from aws lambda function
If you're connecting as a client, which SSH server are you connecting to?
No response
If you're using paramiko as part of another tool, which tool/version?
No response
Expected/desired behavior
I'm using paramiko to connect to an sftp, with public/private key pair. The implementation is done in a lambda function, in aws. Paramiko is added as a layer to the lambda. My implementation for the authentication part:
transport = paramiko.Transport((host, port))
if self.password is not None:
transport.connect(username=username, password=password)
else:
rsa_private_key = paramiko.RSAKey.from_private_key(io.StringIO(private_key), passphrase)
transport.connect(username=self.username, pkey=rsa_private_key)
if transport is not None:
sftp_client = paramiko.SFTPClient.from_transport(transport)
The private_key holds an rsa private key witch starts with
-----BEGIN RSA PRIVATE KEY-----
......
-----END RSA PRIVATE KEY-----
The passphrase is None.
The authentication should pass at this point.
I already tested the key auth with another software, FileZilla and it works.
Actual behavior
Instead of connecting to the sftp server, I'm getting authentication failed.
"message": "AuthenticationException('Authentication failed.')",
"traceback": "Traceback (most recent call last):\n File "/var/task/upload.py", line 49, in main\n transport.connect(username=client_auth_data["username"], pkey=rsa_private_key)\n File "/opt/python/paramiko/transport.py", line 1411, in connect\n self.auth_publickey(username, pkey)\n File "/opt/python/paramiko/transport.py", line 1658, in auth_publickey\n return self.auth_handler.wait_for_response(my_event)\n File "/opt/python/paramiko/auth_handler.py", line 263, in wait_for_response\n raise e\nparamiko.ssh_exception.AuthenticationException: Authentication failed.\n"
I get no other specific error.
How to reproduce
sftp_client = None
transport = None
port = int(client_auth_data["port"]) if "port" in client_auth_data else 22 #default port is 22
try:
transport = paramiko.Transport((client_auth_data["host"], port))
if 'password' in client_auth_data:
transport.connect(username=client_auth_data["username"], password=client_auth_data["password"])
else:
passphrase = client_auth_data["passphrase"] if "passphrase" in client_auth_data else None
rsa_private_key = paramiko.RSAKey.from_private_key(io.StringIO((client_auth_data["private_key"])), passphrase)
transport.connect(username=client_auth_data["username"], pkey=rsa_private_key)
if transport is not None:
sftp_client = paramiko.SFTPClient.from_transport(transport)
except Exception as e:
log.exception({"message": repr(e), "traceback":traceback.format_exc()})
Anything else?
No response
Activity
bskinn commentedon Jul 25, 2023
Hi @TabsB, thanks for the report.
Can you reformat your code snippets and errors and such using code blocks? It's really hard to read without fixed-width code formatting. Thanks!
TabsB commentedon Jul 27, 2023
Sure!
The code:
where client_auth_data is a json containg authentication data.
The error:
masashiozakirivf commentedon Aug 2, 2023
In my environment, transport.connect() without password results to "AuthenticationException('Authentication failed.')"
I set up password on PC I want to connect to.
If you want to know how to connect to non password PC, this information is not useful....
TabsB commentedon Aug 10, 2023
Hello @bskinn,
I was wondering if there are any updates or if you need any additional information from me to proceed.
Thanks!
Tobotimus commentedon Sep 13, 2023
Hi, I'm experiencing the same issue after upgrading from v2.7.2 to v3.3.1, including the context of using paramiko to connect to an SFTP server, with the client running on AWS Lambda.
After inspecting the DEBUG logs, I can see that my issue is due to the server not supporting either RSA2 key verification or the
server-sig-algsextension.I also found a relevant (and very helpful) warning in the changelog entry for 2.9.0, which even suggests a workaround!
Though note that for me, I actually had to specify
disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']}, note the differencekeys->pubkeys.So, my issue had nothing to do with AWS Lambda, and @TabsB your issue might not either. I can't say whether your issue is the same as mine, but it seems likely.
FYI if you want to check the debug logs, you need to do this by configuring the
paramiko.transportlogger using the Python logging module, e.g. this will cause log messages to be printed tosys.stderr:So @TabsB perhaps you can try the following on line 4 of your code:
P.S. Thanks paramiko team for the amazingly helpful and detailed documentation <3
TabsB commentedon Oct 30, 2023
Thanks @Tobotimus for the tips, I will try to use the paramiko debug logs. I think you might be right about the server not supporting the authentication, but I'll dig into that using the debug logs. I did have another sftp server these past days, for which the authentication with public/private key worked smoothly. So, you might be right about the whole thing ;)
babaMar commentedon Dec 8, 2023
Experiencing something similar:
babaMar commentedon Dec 8, 2023
With the same code and
paramiko==2.8.1I have no problems..babaMar commentedon Dec 8, 2023
Looks like this specific line is missing from the logs when it fails:
[2023-12-08, 13:30:23 UTC] {transport.py:1819} DEBUG - Adding ecdsa-sha2-nistp256 host key for sftp.cisco.com: b'2c1ab74fa975ab38000976d6649fe3bc'mnhunter commentedon Jul 30, 2024
Worked on netmiko script if added parametr -
disabled_algorithms={'pubkeys':['rsa-sha2-256', 'rsa-sha2-512']}paramiko ver 3.4.0
example connToSwitch.py
_#!/bin/python
from netmiko import ConnectHandler
import csv
import os
import paramiko
import keyring
#from getpass import getpass
key = paramiko.RSAKey.from_private_key_file(filename="/home/user/.ssh/id_rsa", password="passphrase")
devices = [
{
'device_type': 'eltex',
'host': 'MySwitch', # in /etc/hosts else then ip-addr
'username': 'user',
'ssh_config_file': "~/.ssh/config",
'pkey': key,
'disabled_algorithms':{'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']}
'port': 22,
}
]