pysftp AuthenticationException while connecting to server with private key

13,042

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.

Share:
13,042
heathensoul
Author by

heathensoul

Updated on July 25, 2022

Comments

  • heathensoul
    heathensoul almost 2 years

    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.

  • kidbrax
    kidbrax about 8 years
    So you still have to use the password parameter even though you are using the PEM key? I am currently having the same issue.
  • Sergey Makhonin
    Sergey Makhonin over 4 years
    Here is a decent instruction for converting .ppk to .pem and vice versa: aws.amazon.com/premiumsupport/knowledge-center/…
  • General4077
    General4077 over 4 years
    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 example
  • Martin Prikryl
    Martin Prikryl over 4 years
    Without more information, this is a mere comment to @heathensoul answer, rather than a standalone answer.
  • Martin Prikryl
    Martin Prikryl over 4 years
    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.
  • user3632065
    user3632065 over 4 years
    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.
  • Martin Prikryl
    Martin Prikryl over 4 years
    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.
  • Nicholas Flees
    Nicholas Flees over 3 years
    It would be helpful to know why you needed to downgrade in order to solve the issue.
  • BCArg
    BCArg about 3 years
    I wanted to try that, though that version of pysftp is only compatible with python[version='2.7.*|3.4.*|3.5.*']