繁体   English   中英

来自 Cisco 上的 show 命令的 Paramiko 大型 output

[英]Paramiko large output from show command on Cisco

我使用 Python 3.7 和 Paramiko 2.7.2。 我所有的脚本都运行良好。 但是,当我尝试从 Cisco 上的show命令中保存 output 时,会生成超过 38000 行,而不是保存整个 output。

jhost = paramiko.SSHClient()
jhost.set_missing_host_key_policy(paramiko.AutoAddPolicy())

jhost.connect(host, username=username, password=password, timeout=40)

time.sleep(1)

remote_conn = jhost.invoke_shell()

remote_conn.send("terminal len 0\n")

time.sleep(1)
remote_conn.send("show ip eigrp topology\n")

time.sleep(40)

if remote_conn.recv_ready():
    output = remote_conn.recv(999999)

with open(host +'.txt', 'a') as f1:
    for line in output.decode("utf-8"):
        line = line.replace('\n','')
        f1.write(line)

只保存了大约 15000 行,文件几乎是 1MB。

当然,我正在处理 Paramiko 限制(窗口大小),但我不知道如何解决它。

有任何想法吗?

这不是 Paramiko 的限制。 这是您的代码的限制。 您的代码仅从服务器读取第一块数据。 如果 output 大于第一个块的大小,则您将丢失 rest。

你必须循环调用recv ,直到你读完所有内容。

SSHClient.invoke_shell API(SSH“shell”通道)的问题在于,如果您已经阅读了所有内容,那么您没有可靠的方法来找出答案。 为了自动执行命令,您最好使用SSHClient.exec_command (SSH“exec”通道)。 有关使用SSHClient.exec_command读取大型 output 的可靠 wau,请参阅Paramiko ssh die/hang with big output

尽管思科有其局限性,这可能会使情况复杂化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM