Paramiko ssh 死/挂,输出大 [英] Paramiko ssh die/hang with big output

查看:31
本文介绍了Paramiko ssh 死/挂,输出大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 Paramiko 和 ssh 来备份服务器以调用 tar 命令.当文件数量有限时,一切正常,但当文件夹很大时,脚本会无休止地等待.下面的测试告诉我问题出在标准输出的大小上.

I try to backup a server using Paramiko and ssh to call a tar command. When there is a limited number of files, all works well but when it's a big folder, the script wait endlessly. The following test shows me that the problem comes from the size of the stdout.

有没有办法纠正它并执行这种命令?

Is there a way to correct that and execute this kind of command?

案例大输出:

query = 'cd /;ls -lshAR -h'
chan.exec_command(query)
while not chan.recv_exit_status():
    if chan.recv_ready():
        data = chan.recv(1024)
        while data:
            print data
            data = chan.recv(1024)

    if chan.recv_stderr_ready():
        error_buff = chan.recv_stderr(1024)
        while error_buff:
            print error_buff
            error_buff = chan.recv_stderr(1024)
    exist_status = chan.recv_exit_status()
    if 0 == exist_status:
        break

结果是(不好 - 阻止 - 死??)

Result is (not ok - block - die ??)

2015-07-25 12:57:07,402 --> Query sent

案例小输出:

query = 'cd /;ls -lshA -h'
chan.exec_command(query)
while not chan.recv_exit_status():
    if chan.recv_ready():
        data = chan.recv(1024)
        while data:
            print data
            data = chan.recv(1024)

    if chan.recv_stderr_ready():
        error_buff = chan.recv_stderr(1024)
        while error_buff:
            print error_buff
            error_buff = chan.recv_stderr(1024)
    exist_status = chan.recv_exit_status()
    if 0 == exist_status:
        break

结果是(一切正常)

2015-07-25 12:55:08,205 --> Query sent
total 172K
4.0K drwxr-x---   2 root psaadm 4.0K Dec 27  2013 archives
   0 -rw-r--r--   1 root root      0 Jul  9 23:49 .autofsck
   0 -rw-r--r--   1 root root      0 Dec 27  2013 .autorelabel
4.0K dr-xr-xr-x   2 root root   4.0K Dec 23  2014 bin
2015-07-25 12:55:08,307 --> Query executed (0.10) 

推荐答案

如果 ls -R 打印大量错误输出(如果当前用户不是 rootcode> => 无法访问所有文件夹),您的代码最终会死锁.

If the ls -R prints lots of error output (what is likely if the current user is not root => does not have access to all folders), your code deadlocks eventually.

这是因为,错误流的输出缓冲区最终会填满,所以 ls 停止工作,等待您读取流(清空缓冲区).

It's because, the output buffer of the error stream eventually fills, so the ls stops working, waiting for you to read the stream (empty the buffer).

当您等待常规输出流完成时,它永远不会做的事情,因为 ls 等待您读取错误流,而您永远不会做的事情.

While you wait for the regular output stream to finish, what it never does, as the ls waits for you to read the error stream, what you never do.

您必须并行读取两个流(请参阅使用 Python Paramiko 在不同的 SSH 服务器中并行运行多个命令).

You have to read both streams in parallel (see Run multiple commands in different SSH servers in parallel using Python Paramiko).

或者更简单,使用Channel.set_combine_stderr 将两个流合并为一个.

Or even easier, use the Channel.set_combine_stderr to merge both streams to one.

这篇关于Paramiko ssh 死/挂,输出大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆