user3473406
user3473406

Reputation: 155

Authentication failed pysftp with private key

Using the Pysftp library to sftp to a server, I am getting Authentication failed when attempting to connect.

However using the same credentials in filezilla I am able to connect.

from pysftp import CnOpts, Connection
from paramiko import RSAKey
import io

private_key_text = '''
-----BEGIN OPENSSH PRIVATE KEY-----
   key removed...
-----END OPENSSH PRIVATE KEY-----
'''

    passphrase = 'my_passphrase'
    private_key = RSAKey.from_private_key(io.StringIO(private_key_text), password=passphrase)
    host = 'host_address'
    username = 'username'
    cnopts = CnOpts()
    target_dir = '/home'
    cnopts.hostkeys = None
    with Connection(host=host, username=username, private_key=private_key, private_key_pass=passphrase,
                    cnopts=cnopts, default_path=target_dir) as sftp:
        print(f'Successfully connected to remote SFTP server [{host}]')
    

Upvotes: 2

Views: 5212

Answers (1)

Neil C. Obremski
Neil C. Obremski

Reputation: 20244

Paramiko recently added some code in the 2.9.x which causes an paramiko.ssh_exception.AuthenticationException('Authentication failed.') exception. Try installing paramiko==2.8.1 explicitly and see if the issue still occurs.

See change log notes for 2.9.0 at https://www.paramiko.org/changelog.html

And also this issue here: https://github.com/paramiko/paramiko/issues/1961

Paramiko warning in change log for 2.9.0

Upvotes: 5

Related Questions