python - 当 Cisco 路由器的 ssh 命令产生大量输出时,subprocess.CalledProcessError 和 ssh 连接断开

标签 python python-2.7 subprocess cisco

我的代码有一些问题。当输出较小时它工作正常,但当输出较大时它会损坏。

这是我的代码:

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

当数据较小时,此方法可以正常工作,但当数据较大时,此方法会失败。

这是错误:

subprocess.CalledProcessError: Command 'plink -ssh -l test -pw password -P 4000 10.10.10.1 "list-dev *"' returned non-zero exit status 1

编辑:

我替换了plink并编写了paramiko,但仍然没有获取所有数据。这是代码:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,username=username, password=password, port = 9000)
list =["list-devices rt*"]
command = '\n'.join(list)
print command
stdin,stdout,stderr = ssh.exec_command(command)
print stdout.read()

它给了我以下错误:

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.

最佳答案

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

  • SSH using public key authentication to ... - Cisco Support Community表示一旦 Cisco 在输入上看到 EOF,它就会断开连接的两侧,即使根据 TCP 规则,它应该只关闭输入套接字。它建议的解决方法是延迟 EOF,直到所有输出都已下载。它使用快速而肮脏的sleep,但这对于脚本编写来说是不可靠的。

  • 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,而是恰好包含它的命令后面的附加数据。第一个链接的另一个解决方法是在输入命令之间插入几个换行符 - 以这种方式看待事物,它们充当填充,以代替损坏的传输逻辑否则会插入自身并阻塞的填充。

关于python - 当 Cisco 路由器的 ssh 命令产生大量输出时,subprocess.CalledProcessError 和 ssh 连接断开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46964905/

相关文章:

最近值的 Python 百分位数与先前值的窗口

python - 如何在其他函数可以在 python 中访问的函数中创建变量?

python - Python 系统调用失败

python - 通过 pip 安装本地轮时 ValueError "Expected version spec"

python - 如何将多个变量从Python脚本传递到C shell脚本

python 子进程和 unicode execv() arg 2 必须只包含字符串

python - 删除数据框python中的空间

python - 从子类实例访问父类实例属性?

python - 如何抓取多个网站的缩略图截图?

python - 在 Python 中从 Ascii 转换某些特殊字符时遇到问题