Python Paramiko:读取 SSH 协议横幅时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25609153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Paramiko : Error reading SSH protocol banner
提问by FunkySayu
Recently, I made a code that connect to work station with different usernames (thanks to a private key) based on paramiko.
最近,我制作了一个基于paramiko的连接不同用户名(感谢私钥)工作站的代码。
I never had any issues with it, but today, I have that : SSHException: Error reading SSH protocol banner
我从来没有遇到过任何问题,但是今天,我遇到了: SSHException: Error reading SSH protocol banner
This is strange because it happens randomly on any connections. Is there any way to fix it ?
这很奇怪,因为它在任何连接上随机发生。有什么办法可以解决吗?
采纳答案by TinBane
It depends on what you mean by "fix". The underlying cause, as pointed out in the comments, are congestion/lack of resources. In that way, it's similar to some HTTP codes. That's the normal cause, it could be that the ssh server is returning the wrong header data.
这取决于您所说的“修复”是什么意思。正如评论中所指出的,根本原因是拥塞/缺乏资源。这样,它类似于某些 HTTP 代码。这是正常原因,可能是 ssh 服务器返回了错误的标头数据。
429 Too Many Requests, tells the client to use rate limiting, or sometimes APIs will return 503 in a similar way, if you exceed your quota. The idea being, to try again later, with a delay.
429 Too Many Requests,告诉客户端使用速率限制,或者如果超出配额,有时 API 会以类似的方式返回 503。这个想法是,稍后再试,延迟。
You can attempt to handle this exception in your code, wait a little while, and try again. You can also edit your transport.py file, to set the banner timeout to something higher. If you have an application where it doesn't matter how quickly the server responds, you could set this to 60 seconds.
您可以尝试在代码中处理此异常,稍等片刻,然后重试。您还可以编辑您的 transport.py 文件,将横幅超时设置为更高的值。如果您的应用程序服务器响应速度无关紧要,则可以将其设置为 60 秒。
回答by Moe
When changing the timeout value (as TinBane mentioned) in the transport.py file from 15 to higher the issue resolved partially. that is at line #484:
将 transport.py 文件中的超时值(如 TinBane 提到的)从 15 更改为更高时,问题部分解决。在第 484 行:
self.banner_timeout = 200 # It was 15
However, to resolve it permanently I added a static line to transport.py to declare the new higher value at the _check_banner(self):function.
但是,为了永久解决它,我在 transport.py 中添加了一条静态行以在_check_banner(self):函数中声明更高的新值。
Here is specifically the change:
具体变化如下:
- It was like this:
- 它是这样的:
def _check_banner(self):
for i in range(100):
if i == 0:
timeout = self.banner_timeout
else:
timeout = 2
- After the permanent change became like this:
- 永久更改后变成这样:
def _check_banner(self):
for i in range(100):
if i == 0:
timeout = self.banner_timeout
timeout = 200 # <<<< Here is the explicit declaration
else:
timeout = 2
回答by Greg Dubicki
Adding to the answers above, suggesting to edit transport.py: you don't have to do that anymore.
添加到上面的答案,建议编辑transport.py:您不必再这样做了。
Since Paramiko v. 1.15.0, released in 2015, (this PR, to be precise) you can configure that value when creating Paramiko connection, like this:
自2015 年发布的Paramiko v. 1.15.0以来,(准确地说是这个 PR)您可以在创建 Paramiko 连接时配置该值,如下所示:
client = SSHClient()
client.connect('ssh.example.com', banner_timeout=200)
In the currentversion of Paramiko as of writing these words, v. 2.7.1, you have 2 more timeouts that you can configure when calling connectmethod, for these 3 in total (source):
在编写这些话时的当前版本的 Paramiko 中,v. 2.7.1,您可以在调用connect方法时配置另外 2 个超时,总共有这 3 个(来源):
banner_timeout- an optional timeout (in seconds) to wait for the SSH banner to be presented.timeout- an optional timeout (in seconds) for the TCP connectauth_timeout- an optional timeout (in seconds) to wait for an authentication response.
banner_timeout- 等待 SSH 横幅出现的可选超时(以秒为单位)。timeout- TCP 连接的可选超时(以秒为单位)auth_timeout- 等待身份验证响应的可选超时(以秒为单位)。

