paramiko 结合了 stdout 和 stderr
我正在尝试合并 stdout 和 stderr 的输出。我相信这可以通过 Channel 对象的 set_combine_stderr() 来完成。
这就是我正在做的:
SSH = paramiko.SSHClient()
#I connect and everything OK, then:
chan = ssh.invoke_shell()
chan.set_combine_stderr(True)
chan.exec_command('python2.6 subir.py')
resultado = chan.makefile('rb', -1.)
但是,当我尝试存储结果时出现以下错误(上面的最后一行, chan.makefile() ):
错误:频道已关闭。
任何帮助将不胜感激
I am trying to combine the output of stdout and stderr. My belief is that this can be done with the set_combine_stderr() of a Channel object.
This is what I am doing:
SSH = paramiko.SSHClient()
#I connect and everything OK, then:
chan = ssh.invoke_shell()
chan.set_combine_stderr(True)
chan.exec_command('python2.6 subir.py')
resultado = chan.makefile('rb', -1.)
However, I get the following error when I try to store the result (last line above, chan.makefile() ):
Error: Channel closed.
Any help would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然
set_combine_stderr
确实将stderr
转移到stdout
流,但它是以混乱的顺序进行的,因此您不会得到您可能得到的结果想要,即按写入顺序组合的行,就像在本地终端窗口中运行命令一样。相反,请使用get_pty
。这将导致服务器通过伪终端运行这些行,使它们按时间顺序排列。这是一个测试程序
outerr.py
,它在stdout
和stdin
上交替写入行。假设它位于 llmps@meerkat2 的主目录中。现在尝试以下代码来远程运行它:
如果运行上面的代码,您应该会看到按顺序排列的 100 行。相反,如果您注释掉
chan.get_pty()
调用并取消注释chan.set_combine_stderr(True)
调用,您将得到一堆stdout
code> 和stderr
行在运行之间随机散布。While it is true that
set_combine_stderr
divertsstderr
to thestdout
stream, it does so in chaotic order, so you do not get the result you probably want, namely, the lines combined in the order written, as if you were running the command in a local terminal window. Instead, useget_pty
. That will cause the server to run the lines through a pseudo-terminal, keeping them in chronological sequence.Here's a test program,
outerr.py
, that writes alternating lines onstdout
andstdin
. Assume it's sitting in the home directory of llmps@meerkat2.Now try the following code to run it remotely:
If you run the above, you should see 100 lines in order as written. If, instead, you comment out the
chan.get_pty()
call and uncomment thechan.set_combine_stderr(True)
call, you will get clumps ofstdout
andstderr
lines interspersed randomly from run to run.好吧,我知道这是一个相当老的话题,但我遇到了同样的问题,并且我得到了一个(也许不那么)漂亮的解决方案。只需调用远程服务器上的命令,将 stderr 重定向到 stdout,然后始终从 stdout 读取。例如:
Ok, I know this is quite an old topic, but I run into the same problem and I got a (maybe not-so-)pretty solution. Just call the command on the remote server redirecting the stderr to stdout and then always read from the stdout. For example:
@AaronMcSmooth:我指的是我正在连接的计算机的标准输出和标准错误(通过 SSH)。
我最终这样做了:
出于我的应用程序的目的,区分 stdout 和 stderr 并不重要,但我认为这不是结合两者的最佳方式。
SSHClient.exec_command()
的代码是(查看 paramiko 的源代码):我在通道上执行相同的操作,但收到通道已关闭错误。
@AaronMcSmooth: I am referring to the stdout and stderr of the computer I am connecting to (via SSH).
I ended up doing this:
For the purpose of my application, it doesn't matter to distinguish between stdout and stderr, but I don't think that's the best way to combine the two.
The code of
SSHClient.exec_command()
is (looking at paramiko's source code):I am performing the same actions on the channel but receive the Channel is closed error.