Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SFTP SSHClient functions public #117

Open
tct492 opened this issue Apr 15, 2018 · 2 comments
Open

Make SFTP SSHClient functions public #117

tct492 opened this issue Apr 15, 2018 · 2 comments

Comments

@tct492
Copy link

tct492 commented Apr 15, 2018

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

@pkittenis
Copy link
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.

@pkittenis pkittenis changed the title sftp read to variable Expose more SSH client functions via public API Apr 19, 2018
@pkittenis pkittenis added this to the 2.0.0 milestone Apr 19, 2018
@pkittenis pkittenis removed this from the 2.0.0 milestone Aug 13, 2020
@pkittenis
Copy link
Member

SSHClient functions to make public:

  • _make_sftp
  • _sftp_openfh

@pkittenis pkittenis changed the title Expose more SSH client functions via public API Make SFTP SSHClient functions public Dec 11, 2020
@pkittenis pkittenis added this to the 2.9.0 milestone Nov 16, 2021
@pkittenis pkittenis modified the milestones: 2.9.0, 2.11.0 Apr 1, 2022
@pkittenis pkittenis removed this from the 2.11.0 milestone Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants