Paramiko:ssh.exec_command收集输出说明开放通道作为回应

4 python ssh remote-server paramiko python-2.7

我有一个paramiko和ssh的python脚本,如下所示

import paramiko

# setup ssh connection this works. no problem.
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn = ssh.connect(MACHINEIP, username=ROOTUSER, password=ROOTUSER_PASSWORD, port=22)

# This first ssh exec works perfect. 
(sshin1, sshout1, ssherr1) = ssh.exec_command(cmd1)

# When I print the output of 2nd and 3rd ssh exec, I get output saying of channel open
(sshin2, sshout2, ssherr2) = ssh.exec_command(cmd2)
print sshout2
(sshin3, sshout3, ssherr3) = ssh.exec_command(cmd3)
print sshout3
Run Code Online (Sandbox Code Playgroud)

当exec_command多次使用以收集输出时,输出中的通道打开消息:

<paramiko.ChannelFile from <paramiko.Channel 2 (open) window=2097152 
   -> <paramiko.Transport at 0x1d42bd0L (cipher aes128-ctr, 128 bits) 
(active; 1 open channel(s))>>>

<paramiko.ChannelFile from <paramiko.Channel 6 (open) window=2097152 
   -> <paramiko.Transport at 0x1d42bd0L (cipher aes128-ctr, 128 bits) 
(active; 2 open channel(s))>>>
Run Code Online (Sandbox Code Playgroud)

我该如何关闭这个开放频道?或者对此有何解决方案?我正在使用python 2.7.

小智 11

应该使用as sshout.read()而不是我sshout只在打印时使用.