Python SFTP Connection Fabric files.exist does not find the file

32 views Asked by At

I'm maintaining code written by a retired colleague. This code uses Fabric 2.7.1 to download files from our old FTP server.

from fabric import Connection
from patchwork import files
...
if not files.exists(conn, groups['file']):
    raise FileNotFoundError(f'No such file: {groups["file"]}')
...

This code worked for many years with our old FTP server. We must use SFTP now and the new server no longer allows ssh from internal IPs. I guess the above code used in fact ssh and not ftp as my retired colleague stated.

With the new server, It always runs into the FileNotFoundError, the file and path are fully correct and checked a dozen times. I can see the file on my sftp shell.

I wonder, why a third party library should allow an Fabric object to be handed over? files.exists seems to be a standard python library, not specialized for SFTP. 'I don't fully understand how this could work for years.

I also found this snippet

 if c.run('test -f /opt/mydata/myfile', warn=True).failed:

but this leads to "This service allows sftp connections only."

So, how do I properly check if a files exists with fabric 2.7.1? The docs says there is only a put and a get method. I'm unable to upgrade Fabric as this breaks dependencies. There are so many code parts like this that I need a simple-as-possible approach to get it working again.

1

There are 1 answers

1
Powerriegel On

I found the reason for this behavior:

files.exist relies on ssh method test (GitHub)

This method is just not available on pure SFTP servers. OpenSSH does not allow to configure methods available via SFTP.

Due to chroot it's also difficult to allow SSH access from given list of IPs.

I will never use any code from Fabric. Don't know how I can get my code working, yet. Paramiko itself might be better but it's much work to migrate code to another framework.