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

Using sh in a remote machine via ssh #592

Closed
jordiae opened this issue Nov 21, 2021 · 2 comments
Closed

Using sh in a remote machine via ssh #592

jordiae opened this issue Nov 21, 2021 · 2 comments

Comments

@jordiae
Copy link

jordiae commented Nov 21, 2021

Hi, many thanks for your library, it's very convenient.

I'd like to use it to execute commands in a remote machine via ssh. I'm aware of: https://amoffat.github.io/sh/tutorials/interacting_with_processes.html but I need to keep the ssh connection instead of reconnecting each time. Something like:

from sh import Server

server = Server(host=...)
server.ls()  # Use like sh

Or:

with Server(host=...) as s:
   s.ls()

If you are not planning to implement this feature, could you provide some hints on how should one proceed to adapt sh to it? There are other alternatives that do that such as Pyfra (https://github.com/EleutherAI/pyfra) but I enjoy sh's simplicity and syntax.

Thanks for your time in any case.

@amoffat
Copy link
Owner

amoffat commented Nov 21, 2021

There's no plans for this. It's a cool idea though, but making it work is very non-trivial (and requiring a libssh dependency) and starts to change what ssh was originally designed for, which is launching (local) subprocesses. The best that you'll be able to do is by using sh.ssh and reconnecting for each command.

@amoffat amoffat closed this as completed Nov 22, 2021
@mentalisttraceur
Copy link

Note that ssh (at least the OpenSSH implementation, which is what you're probably getting with the ssh command) supports creating a single connection which is then reused.

The relevant config option is ControlMaster and the relevant command-line option is -M.

Basically, you can build your own context manager on top of sh.ssh pretty easily:

  1. in __enter__,
    1. start one ssh command that connects and starts the master connection - and doesn't run anything on the server, but also doesn't do anything,
    2. leave this command running in the background, and
    3. return sh.ssh with the arguments to reuse the master connection already bound to it (I think this package calls it "baked", but either way it's partial application of arguments).
  2. in __exit__, terminate the ssh master connection process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants