Connect to SFTP with key file using Python pysftp
Asked Answered
E

4

15

I need to connect to a SFTP, download the most recent file, then change the file name and load again to the same SFTP folder and delete the 'original name' file. I have done this with FTP with user and password, however in this case, the SFTP has a key file (.ppk). How can set the key file as password?

Thank you!

import pysftp

srv = pysftp.Connection(host="your_FTP_server", username="your_username",
password="your_password")

# Get the directory and file listing
data = srv.listdir()

# Closes the connection
srv.close()

# Prints out the directories and files, line by line
for i in data:
   print i
Endosperm answered 20/12, 2018 at 20:59 Comment(1)
Nowadays, do not use pysftp in the first place. Use Paramiko directly. See pysftp vs. Paramiko.Grievous
C
21

To connect using a key file, you will want to pass the path to the key file when creating the connection. To do this, you'll set the parameter "private_key" to the path to the file.

Your code above should look something like this:

srv = pysftp.Connection(host="you_FTP_server", username="your_username", private_key="./Path/To/File")

When pySFTP initiates the connection, it will try to use the file you passed in. If it fails because of the keyfile, it will throw an authentication exception.

Here's the link to where I found the answer: https://pysftp.readthedocs.io/en/release_0.2.7/pysftp.html.

Cithara answered 24/12, 2018 at 17:9 Comment(1)
above link is now dead :(Nanette
L
4

It's important to specify that "password" is only used if server has a password. In case is your private key the one protected with password, you must use "private_key_pass" instead.

So to connect to a remote server with public/private key you need:

1) upload public key to server

2) create a connection with your private key + private key password:

srv = pysftp.Connection(host="host", username="username", private_key="file_with_private_key", private_key_pass="password")
Lashoh answered 9/6, 2020 at 10:12 Comment(0)
P
0

I had similar problem and below mentioned steps might help you. It worked for me

  1. Use pysftp Version = 0.2.8
pip install 'pysftp>=0.2.8'
  1. Need to convert the private Key to openSSH format (Use conversion tab in puttyGen and choose Export OpenSSHkey and save the file.Use the same file in python script below)
sftp_client = pysftp.Connection(host='x.x.x.x',username='ftpuser-name ',private_key="filename")
Pachisi answered 3/5, 2022 at 4:49 Comment(0)
I
0

you can use paramiko instead of Pysftp. if you have the private key in putty format, first change that to openssh format. using putty gen. After that, you can pass that openssl private key to paramiko and do sftp.

            # get private key for paramiko
            private_key_content= 'ohik2n3xxxxxxxxxx'
            in_memory_private_key_file = io.StringIO(private_key_content)
            private_key = paramiko.RSAKey.from_private_key(in_memory_private_key_file, password=private_key_passphrase)

            # create SSH client and connect to sftp host
            ssh_client = paramiko.SSHClient()
            ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh_client.connect(sftp_host, username=sftp_user, pkey=private_key, allow_agent=False,  look_for_keys=False,
                               disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']})
            sftp_client = ssh_client.open_sftp()
            sftp_client.chdir(sftp_folder)
            sftp_client.putfo(data, output_filename)  
           ```
Illconsidered answered 29/11, 2023 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.