使用 Twisted 的检修孔编写脚本

Scripting with Twisted's manhole

总结:无法将命令自动化到 Twisted 的沙井。求solutions/advice.

Twisted 有一个很棒的功能,叫做沙井。它允许用户通过 ssh 连接到当前的 运行ning Twisted 服务器和 inspect/interact 及其内部结构。

我想为此编写一些脚本。连接到检修孔只需要

ssh localhost -p 12345

然后用户进入 Python 解释器,可以访问 运行ning 进程。

通常使用 ssh 可以 运行 在远程服务器上执行命令并退出,例如

ssh foo@bar.com 'ls'

将在登录目录执行'ls',然后ssh连接将关闭。

我想执行类似的操作

ssh localhost -p 12345 'print "hello, world"'

到检修孔,但我收到(ssh 冗长):

debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: print "Hello world"
exec request failed on channel 0

无论如何我可以自动化操作沙井?

ssh localhost -p 12345 print 'Hello world' 失败的原因是 "print 'Hello world'" 作为应该执行命令的 "exec" 请求发送。您的服务器(沙井)不支持(显然)。

您需要改为提供 python 解释器标准输入。 例如:

ssh -tt localhost -p 12345 << EOS
print "Hello world"
EOS

注意 -tt 标志 - 它强制 ssh 分配 tty,无论您的输入设备不是 tty。如果没有 -tt,您将收到 "shell request failed on channel" 错误。