1

I'm trying to get the most recent file from an SFTP server without using a loop as the number of files in the directory is thousands (and growing). The file names takes the following format: filename_date.xml (where filename can be any name and date is in the format yyyymmdd_hh.mm.ss).

According to the docs listdir() and listdir_attr() return a list of all files in arbitrary order so this isn't much help.

EDIT: Further to the above I cannot execute shell commands, if I try to run a script using exec_command I get This service allows sftp connections only..

Simon Melouah
  • 642
  • 4
  • 11
  • 24
  • 1
    This isn't a paramiko limitation, it's a SFTP limitation. See the spec at https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13 -- the operations it documents are all the operations that exist. – Charles Duffy Jan 12 '18 at 17:49

1 Answers1

1

There's no better way using a pure SFTP protocol.

For an example of implementation, see:
How to download only the latest file from SFTP server with Paramiko?


You would have to use another interface to retrieve the name of the latest file.

For example, if you have a shell access:

Or you can create web service (web page) on the server that returns the name of the latest file.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    If one controls the server, can just have it maintain a symlink to the latest file; considerably less trouble than setting up a web page, and keeps everything over just one protocol (which is pertinent if the SSH server for which we're using the SFTP subsystem is configured not to honor `shell` or `exec` requests). – Charles Duffy Jan 12 '18 at 17:53
  • Unfortunately I can only run SFTP commands, trying to execute shell commands returns `This service allows sftp connections only.` – Simon Melouah Jan 15 '18 at 10:24
  • OK, then my first sentence stands: *"There's no better way using a pure SFTP protocol."* – Martin Prikryl Jan 15 '18 at 10:26