subprocess.CalledProcessError当对Cisco路由器的ssh命令产生较大的输出时,ssh连接就会断开

2024-09-22 16:38:57 发布

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

我的代码有问题。当输出很小时,它工作得很好,但当输出很大时,它就断了。在

这是我的代码:

def listDevices(username, pass, regex):
    command = "list-dev " + regex
    deviceArray = []
    connectString = "plink -ssh -l " + username + " -pw " + pass + " -P " + SshPort + " " + Server + " \"" + command + "\""
    rawList = subprocess.check_output(connectString, shell=True)
          for line in rawList.split("\r\n"):
              if "" is not line:
                  deviceArray.append(line)
          print deviceArray
          return deviceArray

Server = 10.10.10.1
SshPort = 22 
username = "test"
pass - "password"  
regex = "rt*mdr*"    

mdrList = listDevices(username, pass, regex)
print mdrList

当数据很小时,这种方法工作得很好,但是当数据很大时,这种方法就失败了。在

错误如下:

^{pr2}$

编辑:

我替换了普林克,写了paramiko,但仍然没有得到所有的数据。代码如下:

^{3}$

它给出了以下错误:

Traceback (most recent call last):
  File "C:/Users/xx/Scripts/Test2.py", line 31, in <module>
    stdin,stdout,stderr = ssh.exec_command(command)
  File "C:\Python27\paramiko\client.py", line 404, in exec_command
    chan.exec_command(command)
  File "C:\Python27\paramiko\channel.py", line 60, in _check
    return func(self, *args, **kwds)
  File "C:\Python27\paramiko\channel.py", line 229, in exec_command
    self._wait_for_event()
  File "C:\Python27\paramiko\channel.py", line 1086, in _wait_for_event
    raise e
paramiko.ssh_exception.SSHException: Channel closed.

Tags: 代码inpyparamikoforlineusernamepass
1条回答
网友
1楼 · 发布于 2024-09-22 16:38:57

根据plink ssh not working with multiple commands passed in a file. - 65059 - The Cisco Learning Network,这是思科路由器的问题,因此与Python无关。在

  • SSH using public key authentication to ... - Cisco Support Community表示一旦Cisco在输入上看到EOF,它就会删除连接的两边,即使根据TCP规则,它应该只关闭输入套接字。它建议的一个解决方法是延迟EOF,直到所有输出都下载完毕。它使用了一个quick&dirtysleep,但是对于脚本来说这是不可靠的。

  • Putty Dies with Large Output : networking - Reddit说是MTU问题。症状是无法获取超过一个屏幕的信息:

    I've come across a few MTU-related issues that manifested in terminal emulators in a similar manner. Usually it's some sort of point-to-point leased line that's carried in a VLAN where the added bytes for tagging mess things up and drop the frame in transit. When that happens, shorter output will go through fine, but longer output will just kill the session. Sometimes gracefully, other times not.

事实上,最后一个似乎是正确的解释。触发连接断开的不是EOF,而是命令后恰好包含它的附加数据。第1个链接上的另一个解决方法是在输入命令之间插入两行新行,以这种方式查看内容,它们用作填充,以代替损坏的传输逻辑将插入并阻塞它的内容。在

相关问题 更多 >