Skip to content

Make SFTP SSHClient functions public #117

Closed

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

Activity

  1. pkittenis commented on Apr 18, 2018

    @pkittenis
    Member

    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 👍

    Leaving open for further feedback.

  2. changed the title [-]sftp read to variable[/-] [+]Expose more SSH client functions via public API[/+] on Apr 19, 2018
  3. added this to the 2.0.0 milestone on Apr 19, 2018
  4. removed this from the 2.0.0 milestone on Aug 13, 2020
  5. pkittenis commented on Dec 11, 2020

    @pkittenis
    Member

    SSHClient functions to make public:

    • _make_sftp
    • _sftp_openfh
  6. changed the title [-]Expose more SSH client functions via public API[/-] [+]Make SFTP SSHClient functions public[/+] on Dec 11, 2020
  7. added this to the 2.9.0 milestone on Nov 16, 2021
  8. modified the milestones: 2.9.0, 2.11.0 on Apr 1, 2022
  9. removed this from the 2.11.0 milestone on Aug 1, 2022
  10. pkittenis commented on Jan 21, 2026

    @pkittenis
    Member

    Super-ceded by #410

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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