[英]Paramiko large output from show command on Cisco
I use Python 3.7 and Paramiko 2.7.2.我使用 Python 3.7 和 Paramiko 2.7.2。 All my scripts works perfectly.我所有的脚本都运行良好。 However when I try to save output from a show
command on Cisco which generates more than 38000 lines, not the entire output is saved.但是,当我尝试从 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)
Only around 15000 lines are saved and the file is almost 1MB.只保存了大约 15000 行,文件几乎是 1MB。
For sure I'm dealing with a Paramiko limitation (window size) but I can't figure it out how to fix it.当然,我正在处理 Paramiko 限制(窗口大小),但我不知道如何解决它。
Any ideas?有任何想法吗?
It's not a limitation of Paramiko.这不是 Paramiko 的限制。 It's a limitation of your code.这是您的代码的限制。 Your code reads only the first chunk of data from the server.您的代码仅从服务器读取第一块数据。 If the output is larger, than what fits into the first chunk, you lose the rest.如果 output 大于第一个块的大小,则您将丢失 rest。
You have to keep calling recv
in a loop until you read everything.你必须循环调用recv
,直到你读完所有内容。
Problem with the SSHClient.invoke_shell
API (SSH "shell" channel) is that you have no reliable way to find out, if you have read everything. SSHClient.invoke_shell
API(SSH“shell”通道)的问题在于,如果您已经阅读了所有内容,那么您没有可靠的方法来找出答案。 For automating command execution, you better use SSHClient.exec_command
(SSH "exec" channel).为了自动执行命令,您最好使用SSHClient.exec_command
(SSH“exec”通道)。 For a reliable wau to read large output with use of SSHClient.exec_command
, see Paramiko ssh die/hang with big output .有关使用SSHClient.exec_command
读取大型 output 的可靠 wau,请参阅Paramiko ssh die/hang with big output 。
Though Cisco has its limitations, which may complicate that.尽管思科有其局限性,这可能会使情况复杂化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.