1

I’m trying to retrieve the free available disk space I have on my SFTP server. The SFTP server runs on Windows platform and my SFTP client is running on a Linux Docker container. I am using SFTP Paramiko API. I do not have ssh installed on my docker container. Anyone got any idea?

I’m trying to get the free space of drive C of my SFTP server running on Windows.

This piece of code below runs in my Docker container.

def get_free_space(host, port, user, pwd):
    client = paramiko.client.sshclient()
    client.connect(host, user, pwd)
    stdin, stdout, stderr =    client.exec_command(“df -h” + “C:/“) 
    print(stdout.readlines())
    print(stderr.readlines())

However, there is this error stating

'df' is not recognized as an internal or external command, operable program or batch file.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
qt.qt
  • 19
  • 5

1 Answers1

1

Core SFTP protocol does not have API to obtain free disk space.

There are two SFTP extensions that can do that though.

The standard extension is space-available, but that's not supported by the most widespread SFTP server, the OpenSSH.

On the other hand OpenSSH support its own proprietary extension statvfs@openssh.com.

Paramiko supports neither. But it's not difficult to implement them.

For instance for statvfs@openssh.com:

sftp = client.open_sftp()

path = "/remote/path"

path = sftp._adjust_cwd(path)
t, msg = sftp._request(
    CMD_EXTENDED, "statvfs@openssh.com", path
)

if t != CMD_EXTENDED_REPLY:
    raise SFTPError("Expected extended reply response")

block_size = msg.get_int64()
fundamental_block_size = msg.get_int64()
blocks = msg.get_int64()
free_blocks = msg.get_int64()
available_blocks = msg.get_int64()
file_inodes = msg.get_int64()
free_file_inodes = msg.get_int64()
available_file_inodes = msg.get_int64()
sid = msg.get_int64()
flags = msg.get_int64()
name_max = msg.get_int64()

free_bytes = block_size * free_blocks
print(free_bytes)

Or if you have a shell access to the server, execute respective shell command to obtain the disk space.

See Paramiko: read from standard output of remotely executed command

Though this is not SFTP anymore.

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