【问题标题】:Paramiko Server: Signalling the client that stdout is closedParamiko Server:向客户端发出标准输出已关闭的信号
【发布时间】:2018-05-17 08:29:48
【问题描述】:

尝试在paramiko中实现一个测试服务器无需修改客户端进行测试, 我偶然发现了如何关闭stdout 流的问题,这使得“stdout.read()”不会永远挂起,而不会在客户端变得太低级。到目前为止,我已经能够通过以下方式传达完成的命令(简单文本输出到标准输出)执行:

class FakeCluster(paramiko.server.ServerInterface):
    def check_channel_exec_request(self,channel,command):
        writemessage = channel.makefile("w")
        writemessage.write("SOME COMMAND SUBMITTED")
        writemessage.channel.send_exit_status(0)
        return True

但我还没有找到避免

中的中间两行的方法
_,stdout,_ = ssh.exec_command("<FILEPATH>")
    stdout.channel.recv_exit_status()
    stdout.channel.close()
    print(stdout.read())

这已经是一个很好的解决方法,不必直接调用channel.exec_command(找到here)。 不关闭stdoutstream,我的输出将不会打印,并且服务器上的底层传输也永远保持活动状态。

使用stdout.channel.close() 关闭通道并没有真正的效果,或者使用os.close(writemessage.fileno())(差异解释here)不起作用,因为用于I/O 流的paramiko.channel.ChannelFile 对象“没有属性”文件编号'”。 (详细解释见here。)

另外,直接在服务器端关闭通道会为客户端抛出一个SSHException..

here 提出的解决方案总是会修改客户端,但我通过在实际服务器上使用我的客户端脚本知道,如果没有这些额外的行,它必须是可能的!

【问题讨论】:

    标签: python python-3.x server stdout paramiko


    【解决方案1】:

    check_channel_exec_request 中,一旦发送退出状态,就关闭服务器端的通道,根据协议规范,该规范规定通道在每个执行命令的生命周期内都是活动的,然后在此之后关闭。

    这会导致客户端的channel.eof() 变为True,表示命令已完成并且从通道读取不再挂起。

    def check_channel_exec_request(self,channel,command):
        writemessage = channel.makefile("w")
        writemessage.write("SOME COMMAND SUBMITTED")
        writemessage.channel.send_exit_status(0)
        channel.close()
        return True
    

    查看这个基于 paramiko 的 embedded server for integration testing 已存在多年以供参考 - 它实现了 exec 请求等。根据经验,我建议改用基于嵌入式 OpenSSH 的服务器,example of which can also be found on the same repository。 Paramiko 代码并不是特别没有错误。

    【讨论】:

      【解决方案2】:

      我遇到了与此类似的问题。我们的问题是,我们一退出就关闭了整个会话。显然我们的客户(libssh2)不喜欢这样。因此,每次我们关闭一个频道时,我们都会继续尝试接受一个新频道,直到 transport.is_active() 变为 False

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-29
        • 2014-10-05
        • 2013-03-18
        • 2019-12-03
        • 1970-01-01
        相关资源
        最近更新 更多