【问题标题】:Paramiko: ssh.exec_command to collect output says open channel in responseParamiko:收集输出的 ssh.exec_command 表示打开通道作为响应
【发布时间】:2014-01-06 14:06:29
【问题描述】:

我有一个带有 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

当多次使用 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))>>>

我怎样才能关闭这个开放的频道?或者对此有什么解决方案?我正在使用 python 2.7。

【问题讨论】:

  • 第一个 cmd1 在 cmd2 尝试启动之前没有结束?
  • @eri 它只是一个普通的命令。喜欢ls,所以不应该这样
  • @eri 我确实觉得第一个 ssh.exec 没有按照消息完成或关闭,但不知道如何关闭它们。
  • 尝试读取 sshout1 直到 EOF
  • @eri 解决了找到的解决方案,我把它放在下面作为答案

标签: python python-2.7 ssh remote-server paramiko


【解决方案1】:

应该用作sshout.read(),而我只在打印时使用sshout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    相关资源
    最近更新 更多