1

I use Python 3.7 and 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.

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.

For sure I'm dealing with a Paramiko limitation (window size) but I can't figure it out how to fix it.

Any ideas?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ioan
  • 11
  • 1

1 Answers1

0

It's not a limitation of 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.

You have to keep calling recv in a loop until you read everything.

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. For automating command execution, you better use SSHClient.exec_command (SSH "exec" channel). For a reliable wau to read large output with use of SSHClient.exec_command, see Paramiko ssh die/hang with big output.

Though Cisco has its limitations, which may complicate that.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992