Paramiko问题-执行命令时关闭通道

2024-04-26 14:51:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直试图使用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框作为目标主机的同一脚本会产生正确的结果

代码是借来的,我只是在谷歌上找到了一个片段:

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

我在谷歌上搜索了一段时间,但到目前为止找不到任何有用的东西。

提前谢谢你的帮助。

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

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

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


Tags: thetoinpyhostparamikoconnectstdout
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())
    

可能是序曲不支持SSH exec_命令请求。

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

相关问题 更多 >