Skip to content

Make SFTP SSHClient functions public #117

Open
@tct492

Description

@tct492

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions