0

I am using Python Paramiko to retrieve/search file from an SFTP server. I get all file in the directory.

What I need is specific file from that directory. How do I get it?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
TTG
  • 1
  • 1
  • 1
  • 2

3 Answers3

0

Use Paramiko SFTPClient.get to download a single file:

with paramiko.SSHClient() as ssh:
    ssh.connect(host, username=username, password=password)
    with ssh.open_sftp() as sftp:\
        sftp.get("/remote/path/file.txt", "/local/path/file.txt")

You will also have to deal with the server's host key verification.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
-1

What you need to do is create an ssh client, then execute an ls with piped grep to find your file. such as ls /srv/ftp | grep '^FTP_' to find files in the /srv/ftp directory and start with FTP. Then open an sftp connection and execute the get command to bring the files over.

EDIT: Martin below mentioned that there is a better way to get the directory contents using SFTPClient.listdir() - I have revised to that method. More info in the docs: https://docs.paramiko.org/en/stable/api/sftp.html

Putting all that together looks like

import paramiko

host = ''
port = 22
username = ''
password = ''

with paramiko.SSHClient() as client:
    client.connect(host, port, username, password)  
    with client.open_sftp() as sftp:
        files = sftp.listdir('/srv/ftp')
        for i, file in enumerate(files):
            if file and file.startswith('FTP'):
                sftp.get(f'/srv/ftp/{file}', f'~/ftp/{file}')
                print(f'Moved {file}')

This code is untested but should work. Hope that's clear.

Djones4822
  • 577
  • 3
  • 6
  • 23
  • To list files on SFTP server, use `SFTPClient.listdir(_attr)` . Do not use shell commands! – Martin Prikryl Oct 06 '22 at 18:28
  • Thanks @MartinPrikryl - you're right, that is a better way to get the directory contents. chdir, listdir, and listdir_attr are all relevant methods to the stated objective. The docs have more info for anyone else wondering: https://docs.paramiko.org/en/stable/api/sftp.html – Djones4822 Oct 11 '22 at 14:29
  • Why `for i, file in enumerate(files):`? You never use the `i`. Why not simple `for file in files`? Why `if file`? – Martin Prikryl Oct 20 '22 at 05:25
  • Because it is a snippet from other code, not intended to be used directly. You're welcome to edit it to your styling preference. – Djones4822 Oct 21 '22 at 18:25
-1

Here is an answer in case you need a kind of find using a SFTP connection, not knowing the exact path and name of the file. If it is not what you were looking for, I am sorry.

I made a library named sftputil, based on paramiko, which implements advanced functionalities such as glob. To find a specific file and download it, you can do it this way :

from sftputil import SFTP

sftp = SFTP("hostname", "username", password="password")

# Here we look for a file with a name starting with `foo`
found_files = sftp.glob("directory/foo*")

# Here we look for the file `bar` in any subdirectory
found_files = sftp.glob("directory/*/bar")

# But you can use other patterns of course.

# And now the files can be downloaded
for f in found_files:
    sftp.get(f, "my/local/path")

If you don’t know glob, you should read the python documentation, as this implementation works the same way.

RomainTT
  • 140
  • 5