首页 > 解决方案 > 使用 paramiko over SSH 在交互式 CLI 上执行命令

问题描述

我正在使用 paramiko 包为我的服务器创建 SSH。
我编写了以下代码(我的模块的一部分),它应该执行一些命令并返回它的输出。

def exec_and_get_remote_output(self, cmd, decode=True, splitlines=True, ssh_close=False):
    """
    Execute remote command and return output

    :param cmd: command to execute (string)
    :param decode: perform decoding from binary (bool)
    :param splitlines: perform splitting to get list of lines (bool)
    :param ssh_close: close SSH session after execution (bool)
    :return: command output as sting or list of lines
    """

    if CHECK_NONE(self._client):
        logger.debug('ssh client is None')
        return None

    if not self._opened:
        self.open_connection()

    chan = self._client.get_transport().open_session()
    chan.exec_command(cmd)

    logger.debug('Executed SSH command: {}'.format(cmd))

    raw_output = self.receive_output(chan)

    if decode and splitlines:
        output = self._decode_raw(raw_output)
    elif decode and not splitlines:
        output = self._decode_raw(raw_output, splitlines=False)
    else:
        output = raw_output

    logger.debug('Raw output:\n{}'.format(output))

    # Perform cleanup
    self.cleanup(chan, ssh_close)

    return output

此代码应执行一些命令(作为字符串传递),然后解析通道的输出。
当我用简单的 shell 命令调用这个方法时,我会根据需要得到输出。
[例如:print(self.ssh.exec_and_get_remote_output("ls"))]

现在,当我尝试在此服务器 shell 中运行一些 CLI 进程时,我的问题会调用,该进程应该从 shell 获取输入(用户输入)并将输出返回到 shell(在屏幕上打印),在这种情况下 chan.exec_command 正在返回me None (NoneType)而不是[stdin,stdout,stderr]元组和raw_output变量(通道的输出)也被返回None,因此最终没有打印任何内容,此外,我没有SshException抛出任何异常,因此我知道执行此命令是成功的。
这很奇怪,因为当我以交互方式执行此操作(手动连接到服务器)并运行连接到 shell 的命令时,一切都在终端上打印正常。
[示例:print(self.ssh.get_remote_output("echo -e 'show cable rpd' | /opt/confd/bin/confd_cli --noaaa "))]

我阅读了 exec_command 用户手册,没有看到任何会导致这种行为的东西。

这是我从记录器获得的日志:

DEBUG:paramiko.transport:starting thread (client mode): 0x75a745c0
DEBUG:paramiko.transport:Local version/idstring: SSH-2.0-paramiko_2.4.2
DEBUG:paramiko.transport:Remote version/idstring: SSH-2.0-OpenSSH_7.4p1 Debian-10+deb9u6
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_7.4p1)
DEBUG:paramiko.transport:kex algos:['curve25519-sha256', 'curve25519-sha256@libssh.org', 'ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group16-sha512', 'diffie-hellman-group18-sha512', 'diffie-hellman-group14-sha256', 'diffie-hellman-group14-sha1'] server key:['ssh-rsa', 'rsa-sha2-512', 'rsa-sha2-256', 'ecdsa-sha2-nistp256', 'ssh-ed25519'] client encrypt:['chacha20-poly1305@openssh.com', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-gcm@openssh.com', 'aes256-gcm@openssh.com'] server encrypt:['chacha20-poly1305@openssh.com', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-gcm@openssh.com', 'aes256-gcm@openssh.com'] client mac:['umac-64-etm@openssh.com', 'umac-128-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha1-etm@openssh.com', 'umac-64@openssh.com', 'umac-128@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] server mac:['umac-64-etm@openssh.com', 'umac-128-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha1-etm@openssh.com', 'umac-64@openssh.com', 'umac-128@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Kex agreed: ecdh-sha2-nistp256
DEBUG:paramiko.transport:HostKey agreed: ssh-ed25519
DEBUG:paramiko.transport:Cipher agreed: aes128-ctr
DEBUG:paramiko.transport:MAC agreed: hmac-sha2-256
DEBUG:paramiko.transport:Compression agreed: none
DEBUG:paramiko.transport:kex engine KexNistp256 specified hash_algo <built-in function openssl_sha256>
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Adding ssh-ed25519 host key for [10.40.2.142]:2022: b'c145a1e3b7fc4252387d5930a73cc2c9'
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (password) successful!
DEBUG:framework.ssh_client:SSH connection to 10.40.2.142:2022 is open
DEBUG:framework.ssh_client:This message should appear on the console: 
DEBUG:paramiko.transport:[chan 0] Max packet in: 32768 bytes
DEBUG:paramiko.transport:Received global request "hostkeys-00@openssh.com"
DEBUG:paramiko.transport:Rejecting "hostkeys-00@openssh.com" global request from server.
DEBUG:paramiko.transport:[chan 0] Max packet out: 32768 bytes
DEBUG:paramiko.transport:Secsh channel 0 opened.
DEBUG:framework.ssh_client:Executed SSH command: /opt/confd/bin/confd_cli --noaaa
DEBUG:framework.ssh_client:Attempt #1 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #2 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #3 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #4 to check data in the channel:                                                                                                                                                                                                            
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #5 to check data in the channel:                                                                                                                                                                                                            
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #6 to check data in the channel:                                                                                                                                                                                                            
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #7 to check data in the channel:                                                                                                                                                                                                            
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #8 to check data in the channel:                                                                                                                                                                                                            
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #9 to check data in the channel:                                                                                                                                                                                                            
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #10 to check data in the channel:                                                                                                                                                                                                           
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #11 to check data in the channel:                                                                                                                                                                                                           
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #12 to check data in the channel:                                                                                                                                                                                                           
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #13 to check data in the channel:                                                                                                                                                                                                           
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #14 to check data in the channel:                                                                                                                                                                                                           
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #15 to check data in the channel:                                                                                                                                                                                                           
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #16 to check data in the channel:                                                                                                                                                                                                           
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #17 to check data in the channel:                                                                                                                                                                                                           
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds                                                                                                                                                                                  
DEBUG:framework.ssh_client:Attempt #18 to check data in the channel:                                                                                                                                                                                                           
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #19 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #20 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #21 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #22 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #23 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #24 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #25 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #26 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #27 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #28 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #29 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #30 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #31 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #32 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #33 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #34 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #35 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #36 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #37 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #38 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #39 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #40 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #41 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #42 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #43 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #44 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #45 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #46 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #47 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #48 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #49 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #50 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #51 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #52 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #53 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #54 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #55 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #56 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #57 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #58 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #59 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #60 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #61 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #62 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #63 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #64 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #65 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #66 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #67 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #68 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #69 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #70 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #71 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #72 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #73 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #74 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #75 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #76 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #77 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #78 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #79 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #80 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #81 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #82 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #83 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #84 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #85 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #86 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #87 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #88 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #89 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #90 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #91 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #92 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #93 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #94 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #95 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #96 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #97 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #98 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #99 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Attempt #100 to check data in the channel:
DEBUG:framework.ssh_client:Data is NOT ready yet to be read from channel. Waiting 0.1 seconds
DEBUG:framework.ssh_client:Data is not ready to be read from channel. All 100 attempts are exhausted
DEBUG:framework.ssh_client:ERROR: Unable to get remote output
DEBUG:framework.ssh_client:Raw output:
None
DEBUG:paramiko.transport:[chan 0] EOF sent (0)

标签: pythonsshparamiko

解决方案


paramiko有两种不同 exec_command的功能。您执行的那个不应该返回任何值。使用paramiko.SSHClient.exec_commandwhich 返回(stdin, stdout, stderr)

详情如下:

http://docs.paramiko.org/en/2.4/api/channel.html?highlight=exec_command http://docs.paramiko.org/en/2.4/api/client.html?highlight=exec_command

文档字符串:

>>> help(paramiko.channel.Channel.exec_command)
Help on method exec_command in module paramiko.channel:

exec_command(self, *args, **kwds) unbound paramiko.channel.Channel method
    Execute a command on the server.  If the server allows it, the channel
    will then be directly connected to the stdin, stdout, and stderr of
    the command being executed.

    When the command finishes executing, the channel will be closed and
    can't be reused.  You must open a new channel if you wish to execute
    another command.

    :param str command: a shell command to execute.

    :raises:
        `.SSHException` -- if the request was rejected or the channel was
        closed



>>> help(paramiko.SSHClient.exec_command)
Help on function exec_command in module paramiko.client:

exec_command(self, command, bufsize=-1, timeout=None, get_pty=False, environment=None)
    Execute a command on the SSH server.  A new `.Channel` is opened and
    the requested command is executed.  The command's input and output
    streams are returned as Python ``file``-like objects representing
    stdin, stdout, and stderr.

    :param str command: the command to execute
    :param int bufsize:
        interpreted the same way as by the built-in ``file()`` function in
        Python
    :param int timeout:
        set command's channel timeout. See `.Channel.settimeout`
    :param dict environment:
        a dict of shell environment variables, to be merged into the
        default environment that the remote command executes within.

        .. warning::
            Servers may silently reject some environment variables; see the
            warning in `.Channel.set_environment_variable` for details.

    :return:
        the stdin, stdout, and stderr of the executing command, as a
        3-tuple

    :raises: `.SSHException` -- if the server fails to execute the command

推荐阅读