heathensoul
heathensoul

Reputation: 311

pysftp AuthenticationException while connecting to server with private key

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.

Upvotes: 11

Views: 21253

Answers (5)

l001d
l001d

Reputation: 962

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)

Upvotes: 0

user3632065
user3632065

Reputation: 1

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')

Upvotes: -3

Bischiz
Bischiz

Reputation: 1

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

Upvotes: -2

heathensoul
heathensoul

Reputation: 311

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.

Upvotes: 18

Hamid
Hamid

Reputation: 33

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.

Upvotes: 0

Related Questions