Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a python module which provides entry points that wrap the openssh client?

Is there a Python module which provides provides remote control functions by launching subprocesses implementing the ssh command line interface? The kind of functions that would be useful include:

  • run this command on that machine, and return exit code, stdout and stderr or raise an exception after a timeout
  • test whether a directory exists on that machine
  • write these bytes to that filename on that machine

I am aware of paramiko and Conch. However, I would like programs using this module to be able to use the openssh ControlMaster feature that reuses long running existing ssh connections to target machines. Otherwise the SSH connection set up time can dominate run time. This approach also allows other programs that implement the openssh command line interface to be used without rewriting the programs using the module.

like image 980
Dickon Reed Avatar asked May 30 '26 14:05

Dickon Reed


2 Answers

You might find Python's Fabric right for your needs. It looks like it uses paramiko but it is smart about caching connections, as documented in the execution model.

It won't use an existing long-running connection though.

like image 181
Emil Sit Avatar answered Jun 01 '26 05:06

Emil Sit


Some of what you're talking about sounds like you want to use rsync, specifically your functions of "test whether a directory exists on that machine", "write these bytes to that filename on that machine", "run a callback every time lines gets written to that file on that machine". You could either use some of the existing Python-rsync bridges people have provided, or create one of your own.

There is no way you'll be able to keep a long-running SSH tunnel open without having a daemon process to maintain it. Both paramiko and Conch support SSH channels, which could easily be run in a daemon process just like a classic SSH port redirection.

like image 24
jathanism Avatar answered Jun 01 '26 04:06

jathanism



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!