pysftp AuthenticationException while connecting to server with private key
Asked Answered
B

5

10

I am trying to connect to SFTP server. I have a private key along with a password. I have tried to read related questions on SO, but have been unable to successfully connect.

This is what I have tried:

pysftp.Connection(host=<hostname>, username=<username>,
                  password=<password>, private_key=<path to .ppk file>)

AuthenticationException: Authentication failed

pysftp.Connection(host=<hostname>, username=<username>,
                  private_key_pass=<password>, private_key=<path to .ppk file>) 

SSHException: not a valid DSA private key file

However, I can use the same credentials and connect with FileZilla. FileZilla asked for password and converted the .ppk file into an unprotected file.

I tried to use the same host name, username and key file as used in FileZilla, but I continue getting errors. Also tried connecting using Paramiko.

Brachy answered 4/12, 2015 at 20:42 Comment(0)
B
17

I could finally connect.

Converted the file to a .pem file using PuTTY. Passed this .pem file and kept the rest of the parameters the same as before.

pysftp.Connection(host='hostname', username='username',
                   password='password', private_key='path to .pem file')

Hope this helps someone having similar issues.

Brachy answered 4/12, 2015 at 21:10 Comment(3)
So you still have to use the password parameter even though you are using the PEM key? I am currently having the same issue.Tachometer
Here is a decent instruction for converting .ppk to .pem and vice versa: aws.amazon.com/premiumsupport/knowledge-center/…Rockbottom
As Martin Prikryl pointed out you can't use password with private_key. This likely should be private_key_pass=<password> as in the original code exampleAvow
K
0

I solved this problem by downgrading from pysftp0.2.9 to pysftp 0.2.8

pip install pysftp==0.2.8

I used private key with private key password in the connection string like this:

import pysftp as sftp
import sys
srver = sftp.Connection(host='xx.xxx.xx.xx',username='xxxxx',password='xxx',port=9999,private_key='C:\\Users\xxxx\xxx\id_rsa.ppk',private_key_pass='xxxxxx')

Remember that port is actually a number not a string. This solution will work for all those who want to connect using private key and private key password with host name, username and user password.

Konikow answered 28/5, 2018 at 15:49 Comment(2)
It would be helpful to know why you needed to downgrade in order to solve the issue.Guienne
I wanted to try that, though that version of pysftp is only compatible with python[version='2.7.*|3.4.*|3.5.*']Rigby
P
0

I had the same problem on a Linux environment and I was trying to follow the solution from the accepted answer. The first problem I had was converting the .ppk file to a .pem file. I find on a Debian environment, we can convert the .ppk file to a .pem file using PuTTY tools

$ sudo apt-get install putty-tools
$ puttygen abc.ppk -O private-openssh -o abc.pem

The second problem I had with trying out the accepted answer was an Authentication Error, I used private_key_pass instead of password and I was able to make the connection.

cnopts = pysftp.CnOpts()

cnopts = modify_cnopts_as_you_wish(cnopts)

srv = pysftp.Connection(host="hostname", username="user",
                        private_key='path_to_abc.pem',
                        private_key_pass="password", 
                        cnopts=cnopts)
Pamilapammi answered 1/2, 2021 at 10:48 Comment(0)
C
-2

You could directly use the option -m PEM when you add the key with ssh-keygen from your linux console instead of using Putty.

ssh-keygen -t rsa -m PEM
Cisco answered 1/9, 2019 at 10:17 Comment(1)
Without more information, this is a mere comment to @Brachy answer, rather than a standalone answer.Liu
S
-3

I was able to solve same issue with

ssh-keygen -t rsa -m PEM" command and 
pysftp.Connection(host='hostname', username='username',
                   private_key_pass='password', private_key='path to .pem file')
Subedit answered 18/9, 2019 at 15:30 Comment(3)
I do not see how does this answer the question. You have shown how to generate a new key. The question about problems with using an existing key.Liu
In my case problem was with my private key, the how I was generating that key was not support by pysftp and I was able to solve with new key that I have generated in this way.Subedit
But you cannot just generate a new key. You have to put its public key on the server, what your answer does not even mention. And why doing it this way, if you can simply convert the existing key to the format that pysftp supports.Liu

© 2022 - 2024 — McMap. All rights reserved.