【问题标题】:Paramiko Issues - Channel Closed when Executing a CommandParamiko 问题 - 执行命令时通道关闭
【发布时间】:2020-10-02 00:33:59
【问题描述】:

我一直在尝试使用 paramiko 在 Overture Box 上运行命令来建立 SSH 连接。

我的脚本确实正确连接到盒子,没有发现问题,当脚本运行 exec_command(cmd) 方法时出现问题:

Traceback (most recent call last):
  File "test.py", line 39, in <module>
    stdin, stdout, stderr = ssh.exec_command("version")
  File "/opt/csw/lib/python2.7/site-packages/paramiko/client.py", line 345, in exec_command
    chan.exec_command(command)
  File "/opt/csw/lib/python2.7/site-packages/paramiko/channel.py", line 60, in _check
    return func(self, *args, **kwds)
  File "/opt/csw/lib/python2.7/site-packages/paramiko/channel.py", line 229, in exec_command
    self._wait_for_event()
  File "/opt/csw/lib/python2.7/site-packages/paramiko/channel.py", line 1086, in _wait_for_event
    raise e
paramiko.ssh_exception.SSHException: Channel closed.

使用 Linux Box 作为目标主机的相同脚本会产生正确的结果

代码是借来的,我随便搜了一下sn-p:

import sys
import time
import select
import paramiko

host = raw_input("Hostname?: ")
i = 1
password = raw_input("Password?: ")


#
# Try to connect to the host.
# Retry a few times if it fails.
#
while True:
    print "Trying to connect to %s (%i/30)" % (host, i)

    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(host, port=22, username="username", password=password)
        print "Connected to %s" % host
        break
    except paramiko.AuthenticationException:
        print "Authentication failed when connecting to %s" % host
        sys.exit(1)
    except:
        print "Could not SSH to %s, waiting for it to start" % host
        i += 1
        time.sleep(2)

    # If we could not connect within time limit
    if i == 30:
        print "Could not connect to %s. Giving up" % host
        sys.exit(1)

# Send the command (non-blocking)
stdin, stdout, stderr = ssh.exec_command("version")

# Wait for the command to terminate
while not stdout.channel.exit_status_ready():
    # Only print data if there is data to read in the channel
    if stdout.channel.recv_ready():
        rl, wl, xl = select.select([stdout.channel], [], [], 0.0)
        if len(rl) > 0:
           # Print data from stdout
            print stdout.channel.recv(1024),

#
# Disconnect from the host
#
print "Command done, closing SSH connection"
ssh.close()

我发现了其他问题,据说该问题可能与主机中的 SFTP 设置有关,但我能够做到:

sftp username@hostname

我已经用 Google 搜索了一段时间,但目前找不到任何有用的信息。

提前感谢您的帮助。

编辑:差点忘了,我知道问题出在:

stdin, stdout, stderr = ssh.exec_command("version")

我以交互方式运行了整个脚本,当我运行该行时它会中断。

【问题讨论】:

  • 它在 wait_for_event 中失败 - 它是立即失败,还是等待一段时间超时?如果您通过 SSH 登录并正常运行,version 会做什么 - 它只是一行数字,还是更多?它是否提示按键或任何东西? Overture 是运行 Linux 还是运行一些自定义操作系统 - 您能否将版本的输出重定向到文件并查看命令是否运行?
  • 立即生效。如果我通过终端登录设备并运行它,它就可以工作。这只是一条线。它不提示。我相信它运行的是基于 UNIX 的操作系统,但不是 100% 肯定。最后一个问题没看懂,不好意思。你能解释一下吗?

标签: python paramiko


【解决方案1】:

可能是 Overture 不支持 SSH exec_command 请求。

我需要使用transport.open_session()session.get_pty()session.invoke_shell() 来设置交互式shell 会话,然后使用session.send()session.recv() 来写入/读取Cisco 交换机和其他网络的shell 会话电器,当exec_command() 不可用时。

【讨论】:

  • 我会试试这个,我会让你知道它是怎么回事。非常感谢您的建议!
  • 好的,我会接受你的回答。我仍然无法使其工作,但这是由于我对 Channel 类的工作方式缺乏了解。至少现在我可以将命令发送到设备,问题是没有读回所有内容。无论如何,谢谢!
  • 我的原始答案可能有点误导。您应该在通道变量中获得session.invoke_shell() 的返回值——它返回一个 paramiko.Channel 对象。正是这个类似套接字的对象,您可以执行 send 和 recv 调用来控制底层交互式 shell 会话。在执行channel.send('version\n')(或任何命令文本)之后,您应该延迟一小段时间,或者在执行channel.recv(len) 之前检查channel.recv_ready()
  • exec_command() 调用不同,您不会获得明确的状态代码,或指示该过程已完成,您只需要读取 shell 输出,直到您希望返回提示细绳。当支持exec_command() 时,事情变得容易多了:)
  • 你能举个例子吗?
【解决方案2】:

这里有一个解决方法,

  1. 为您的连接获取传输。
  2. 打开一个会话。
  3. 然后exec_command(command) and thenrecv()`,不需要休眠。

    client.connect("host", port=22, username="user",password= "pass",look_for_keys=False, allow_agent=False)
    
    transport=client.get_transport()
    channel = t.open_session()
    
    channel.exec_command(command)
    out = channel.recv(-1)
    print(out.decode())
    

【讨论】:

    最近更新 更多