尝试在中实现测试服务器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 https://stackoverflow.com/questions/3562403/how-can-you-get-the-ssh-return-code-using-paramiko)。
不关闭stdout
流,我的输出将不会打印,并且服务器上的底层传输也永远保持活动状态。
关闭通道stdout.channel.close()
并没有真正产生效果,或者使用os.close(writemessage.fileno())
(差异解释here https://stackoverflow.com/questions/25337320/different-ways-of-closing-a-stream-in-python)不起作用,因为paramiko.channel.ChannelFile
用于 I/O 流的对象“没有属性‘fileno’”。 (详细解释已找到here https://stackoverflow.com/questions/44503787/use-paramikos-stdout-as-stdin-with-subprocess.)
此外,直接在服务器端关闭通道会引发SSHException
为客户..
提出的解决方案here https://stackoverflow.com/questions/35266753/paramiko-python-module-hangs-at-stdout-read总是修改客户端,但我从在实际服务器上使用我的客户端脚本知道,没有这些额外的行一定是可能的!