在 Twisted 中通过 ssh 运行远程命令的最佳方式?
我有一个扭曲的应用程序,现在需要监视在多个盒子上运行的进程。我手动执行的方式是“ssh 和 ps”,现在我希望我的扭曲应用程序执行此操作。我有两个选择。
使用 paramiko
或利用 twisted.conch
我真的很想使用 twisted.conch
但我的研究让我相信它的主要目的创建 SSHServer 和 SSHClient。然而,我的要求是一个简单的 remoteExecute(some_cmd)
我能够弄清楚如何使用 paramiko
来做到这一点,但我不想坚持paramiko
在我的扭曲应用程序中,在查看如何使用 twisted.conch
执行此操作之前,
使用 twisted
来了解如何使用 ssh 运行 remote_cmds
的代码片段将非常有用赞赏。谢谢。
I have a twisted application which now needs to monitor processes running on several boxes. The way I manually do is 'ssh and ps', now I'd like my twisted application to do. I have 2 options.
Use paramiko
or leverage the power of twisted.conch
I really want to use twisted.conch
but my research led me to believe that its primarily intended to create SSHServers and SSHClients. However my requirement is a simple remoteExecute(some_cmd)
I was able to figure out how to do this using paramiko
but I didnt want to stickparamiko
in my twisted app before looking at how to do this using twisted.conch
Code snippets using twisted
on how to run remote_cmds
using ssh would be highly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
后续 - 令人高兴的是,我在下面提到的问题现已解决。更简单的 API 将包含在 Twisted 的下一版本中。最初的答案仍然是使用 Conch 的有效方法,并且可能会揭示一些有关正在发生的事情的有趣细节,但从 Twisted 13.1 及更高版本开始,如果您只想运行命令并处理它的 I/O,这个更简单的界面将起作用。
不幸的是,使用 Conch 客户端 API 在 SSH 上执行命令需要大量代码。 Conch 可以让您处理许多不同的层,即使您只想要合理的、无聊的默认行为。然而,这当然是可能的。以下是我一直想完成并添加到 Twisted 中以简化此情况的一些代码:
需要注意的一些事项:
_CommandTransport.verifyHostKey
是您实现该功能的地方。查看twisted/conch/client/default.py
以获得有关您可能想做的事情的一些提示。$USER
作为远程用户名,您可能希望将其作为参数。_CommandTransport
位于底部,是一个实现 SSH 传输协议的普通旧协议。它创建了一个..._CommandConnection
实现协议的 SSH 连接协商部分。完成后,..._CommandChannel
用于与新打开的 SSH 通道进行通信。_CommandChannel
执行实际的 exec 来启动您的命令。一旦通道打开,它就会创建一个......的实例StdoutEcho
,或您提供的任何其他协议。该协议将从您执行的命令中获取输出,并可以写入命令的标准输入。请参阅 http://twistedmatrix.com/trac/ticket/4698 了解 Twisted 在支持方面的进展这用更少的代码。
Followup - Happily, the ticket I referenced below is now resolved. The simpler API will be included in the next release of Twisted. The original answer is still a valid way to use Conch and may reveal some interesting details about what's going on, but from Twisted 13.1 and on, if you just want to run a command and handle it's I/O, this simpler interface will work.
It takes an unfortunately large amount of code to execute a command on an SSH using the Conch client APIs. Conch makes you deal with a lot of different layers, even if you just want sensible boring default behavior. However, it's certainly possible. Here's some code which I've been meaning to finish and add to Twisted to simplify this case:
Some things to note about it:
reactor.connectTCP
, but I did it as an endpoint to make it more useful; endpoints can be swapped easily without the code that actually asks for a connection knowing._CommandTransport.verifyHostKey
is where you would implement that. Take a look attwisted/conch/client/default.py
for some hints about what kinds of things you might want to do.$USER
to be the remote username, which you may want to be a parameter.SSHUserAuthClient
and overridegetPassword
to do something._CommandTransport
is at the bottom, a plain old protocol that implements the SSH transport protocol. It creates a..._CommandConnection
which implements the SSH connection negotiation parts of the protocol. Once that completes, a..._CommandChannel
is used to talk to a newly opened SSH channel._CommandChannel
does the actual exec to launch your command. Once the channel is opened it creates an instance of...StdoutEcho
, or whatever other protocol you supply. This protocol will get the output from the command you execute, and can write to the command's stdin.See http://twistedmatrix.com/trac/ticket/4698 for progress in Twisted on supporting this with less code.