Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download only the latest file from SFTP server with Paramiko?

I want to write script that connects to my university SFTP server and downloads the latest file with exercises. So far I've changed a little bit the code from Paramiko example, but I do not know how to download the latest file.

Here is my code :

import functools
import paramiko 

class AllowAnythingPolicy(paramiko.MissingHostKeyPolicy):
    def missing_host_key(self, client, hostname, key):
        return

adress = 'adress'
username = 'username'
password = 'password'

client = paramiko.SSHClient()
client.set_missing_host_key_policy(AllowAnythingPolicy())
client.connect(adress, username= username, password=password)

def my_callback(filename, bytes_so_far, bytes_total):
    print ('Transfer of %r is in progress' % filename) 

sftp = client.open_sftp()
sftp.chdir('/directory/to/file')
for filename in sorted(sftp.listdir()):
    if filename.startswith('Temat'):
        callback_for_filename = functools.partial(my_callback, filename)
        sftp.get(filename, filename, callback=callback_for_filename)

client.close() 
like image 888
Cosaquee Avatar asked Jun 03 '15 17:06

Cosaquee


People also ask

How do I download from Paramiko?

Connect : Connect to sftp using correct credentials. Upload: Upload file in specific remote path. Download : Download the file if it exists.

How do I download from SFTP server using python?

To download a remote file from the server using pysftp, we have to open a connection and from the sftp instance and use the get method that expects the path of a remote file that will be downloaded, and the second argument as a local path where the file should be stored.

What is download via SFTP?

SFTP (SSH File Transfer Protocol) is secured protocol to transfer files between local and remote server. To required SSH server running on the remote system. This protocol encrypts the transfer of data between local and remote system. As SFTP provides secure data transfer, so we recommend it over FTP protocol.


2 Answers

Use the SFTPClient.listdir_attr instead of the SFTPClient.listdir to get listing with attributes (including the file timestamp).

Then, find a file entry with the greatest .st_mtime attribute.

The code would be like:

latest = 0
latestfile = None

for fileattr in sftp.listdir_attr():
    if fileattr.filename.startswith('Temat') and fileattr.st_mtime > latest:
        latest = fileattr.st_mtime
        latestfile = fileattr.filename

if latestfile is not None:
    sftp.get(latestfile, latestfile)

For a more complex example, see How to get the latest folder that contains a specific file of interest in Linux and download that file using Paramiko in Python?

like image 89
Martin Prikryl Avatar answered Nov 15 '22 10:11

Martin Prikryl


import paramiko
remote_path = '/tmp'

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=<IP>, username=<USER>, password=<PW>,allow_agent=False)
sftp_client = ssh_client.open_sftp()

sftp_client.chdir(remote_path)
for f in sorted(sftp_client.listdir_attr(), key=lambda k: k.st_mtime, reverse=True):
    sftp_client.get(f.filename, f.filename)
    break

sftp_client.close()
ssh_client.close()

This will connect to the remote server (IP) using password (PW) as user (USER) & downloads the latest file available under <remote_path>

like image 24
Nikil Kumar Avatar answered Nov 15 '22 08:11

Nikil Kumar