Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to SFTP with key file using Python pysftp

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
like image 548
rubio119 Avatar asked Dec 20 '18 20:12

rubio119


People also ask

What is SFTP and how to use it in Python?

SFTP is a simple and fairly reliable way to share the information within the organization. Let’s look at the situation when you need to pick up some files from a remote host with authorization by public key. And after that, let’s see how to use it with in python.

How do I connect using a key file in pysftp?

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. 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.

How to use cnopts with pysftp?

cnopts = pysftp.CnOpts (knownhosts='known_hosts') with pysftp.Connection (host, username, password, cnopts=cnopts) as sftp: where the known_hosts contains a server public key (s)] in a format like: example.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQAB... If you do not want to use an external file, you can also use

How do I install pysftp on Ubuntu terminal?

Installation Of Pysftp Make sure you have established an Internet connection and that pip is already installed on your system. Let’s then install pysftp: Open command line terminal Run command “pip install pysftp” Run Python Try to import pysfpt. If it succeeds means pysftp is now installed on your system


2 Answers

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.

like image 165
b13rg Avatar answered Sep 22 '22 06:09

b13rg


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")
like image 33
quim Avatar answered Sep 22 '22 06:09

quim