是否有一个Python模块可以通过启动实现ssh命令行接口的子进程来提供远程控制功能?有用的函数类型包括:
- 在该机器上运行此命令,并返回退出代码、stdout 和 stderr 或在超时后引发异常
- 测试该机器上是否存在目录
- 将这些字节写入该机器上的该文件
名我知道 paramiko 和 海螺。但是,我希望使用此模块的程序能够使用 openssh ControlMaster 功能重用与目标机器的长时间运行的现有 ssh 连接。否则,SSH 连接建立时间可能会主导运行时间。这种方法还允许使用其他实现 openssh 命令行界面的程序,而无需使用该模块重写程序。
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.
发布评论
评论(3)
您可能会发现 Python 的 Fabric 适合您的需求。它看起来像使用 paramiko,但它对于缓存连接很聪明,如 执行模型。
但它不会使用现有的长期运行的连接。
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.
你所说的一些内容听起来像是你想使用 rsync,特别是你的“测试该机器上是否存在目录”、“将这些字节写入该机器上的该文件名”、“每行运行回调”的功能被写入该机器上的该文件”。您可以使用人们提供的一些现有的Python-rsync桥,或者创建你自己的一个。
如果没有守护进程来维护,您将无法保持长期运行的 SSH 隧道打开。 paramiko 和 Conch 都支持 SSH 通道,可以轻松地在守护进程中运行,就像经典的 SSH 端口重定向一样。
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.
我仍然没有找到任何可以使用的代码,所以编写了下面的版本。我仍然热衷于找到一个可以完成此类任务的开源模块。
I still haven't found any code I can use so wrote the version below. I'm still keen to find an open source module that does this kind of thing.