Make SFTP SSHClient functions public
It would be neat to be able to read a remote file to a variable.
Here's my hacky implementation:
from pssh.ssh2_client import SSHClient
from pssh.native._ssh2 import wait_select
from pssh.constants import DEFAULT_RETRIES, RETRY_DELAY
from ssh2.error_codes import LIBSSH2_ERROR_EAGAIN
from ssh2.sftp import LIBSSH2_FXF_READ, LIBSSH2_SFTP_S_IRUSR
class MySSH2Client(SSHClient):
def __init__(self, host,
user=None,
password=None,
port=None,
pkey=None,
num_retries=DEFAULT_RETRIES,
retry_delay=RETRY_DELAY,
allow_agent=True,
timeout=None):
SSHClient.__init__(self, host, user, password, port, pkey, num_retries, retry_delay, allow_agent, timeout)
def read(self, remote_file, sftp=None):
sftp = self._make_sftp() if sftp is None else sftp
text = ""
with self._sftp_openfh(
sftp.open, remote_file,
LIBSSH2_FXF_READ, LIBSSH2_SFTP_S_IRUSR) as remote_fh:
for size, data in remote_fh:
if size == LIBSSH2_ERROR_EAGAIN:
wait_select(self.session)
continue
text += data
return text
Best Wishes,
Clay
Hi there,
Thank you for the interest. There are other ways to do this as well, for example running a command like cat <file> and reading stdout, though that will be subject to encoding so not appropriate for binary files.
It might be useful for SFTP related functions in SSHClient and ssh2-python helper functions to be made public and have them documented so functionality can be more easily extended. Can take that up for later releases. PRs are welcome as well!
However, since the above use case is for a single host, it is out of scope for parallel-ssh and is a better fit for client-side sub-classing or custom functions as above.
That should be more easy to do without having to use internal API functions which may change and some documentation on how to do in parallel - the primary goal of this project. Good work finding out how to do it despite lack of documentation and public API by the way :+1:
Leaving open for further feedback.
SSHClient functions to make public:
_make_sftp_sftp_openfh