问题描述
我正在构建一个应用程序,其中它通过SSH连接到Cisco设备以收集show
命令的输出.我正在python中使用paramiko模块来完成此操作.
I am building an application where it SSHs to Cisco devices to collect output of show
commands. I am using paramiko module in python to get this done.
在将命令输出与plink
的输出进行比较时,知道paramiko的输出被截断了.尝试使用无缓冲和增加的缓冲区大小,但没有帮助.后来,尝试使用window_size
参数,它似乎可以正常工作.
While comparing the command output with that of plink
, got to know that the output from paramiko is truncated. Tried with unbuffering and increased buffer size and it didn't help. Later, just tried with window_size
parameter and it seems to work.
下面是我的代码:
import paramiko
sshclient = None
try:
sshclient = paramiko.SSHClient()
sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
sshclient.connect('mydevice', username='admin', password='admin12345')
chan = sshclient.get_transport().open_session(window_size=500000)
chan.settimeout(10800)
chan.exec_command('show tech-support fcip')
value = chan.recv(1024)
while value:
print(value)
value = chan.recv(1024)
finally:
if sshclient:
sshclient.close()
按照运输,default_window_size=2097152
;比默认值小1597152.
As per paramiko document for Transport, default_window_size=2097152
; 1597152 lesser than the default_value.
另外,使用default_window_size=2097152
的部分日志输出是:
Also the partial log output with default_window_size=2097152
is:
Authentication (password) successful!
[chan 0] Max packet in: 32768 bytes
[chan 0] Max packet out: 32768 bytes
Secsh channel 0 opened.
[chan 0] Sesch channel 0 request ok
EOF in transport thread
与window_size=500000
相同的是:
Authentication (password) successful!
[chan 0] Max packet in: 32768 bytes
[chan 0] Max packet out: 32768 bytes
Secsh channel 0 opened.
[chan 0] Sesch channel 0 request ok
[chan 0] EOF received (0)
EOF in transport thread
在这里,当window_size为默认值时,甚至在服务器发出终止信号之前,通道也已关闭.
Here it looks like, when the window_size is default value, the channel is being closed even before the termination signal from the server.
专家,请告诉我SSH中window_size
减少的后果,它将如何影响我的应用程序?
Experts, please advise me the consequences of reduced window_size
in SSH and how will it affect my application?
推荐答案
通过修改window_size,可以影响输入返回字符之前一行的最大长度.如果您的应用程序要求您执行一些解析,并且您想要调用
By modifying the window_size, you effect the maximum length a line can be before a return character is entered. If your application requires you to perform some parsing, and you want to call something like
value.split('\n')
您可能会发现字符串分割的频率比您预期的要多/少.
You may find that the strings are being split more/less frequently than you predicted.
这篇关于SSH窗口大小如何影响paramiko的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!