paramiko 不兼容的 ssh 对等体(没有可接受的 kex 算法)

Posted

技术标签:

【中文标题】paramiko 不兼容的 ssh 对等体(没有可接受的 kex 算法)【英文标题】:paramiko Incompatible ssh peer (no acceptable kex algorithm) 【发布时间】:2011-11-09 08:35:46 【问题描述】:

尝试使用 paramiko 库通过 ssh 连接到 Cisco ACS 设备时出现以下错误。我在 python 中使用 paramiko 没有问题,我可以从命令行 ssh 到这个框,或者使用 putty 没有问题。我已经打开调试并在此处复制了信息。如果你能帮助我,请告诉我。

import paramiko
import sys
import socket

try:
    paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
    sshConnection = paramiko.SSHClient()
    sshConnection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    sshConnection.connect('server',username='username',password='password')
except paramiko.BadAuthenticationType:
    sys.stdout.write('Bad Password!\n')     
    sys.exit()
except paramiko.SSHException, sshFail:
    sys.stdout.write('Connection Failed!\n')
    sys.stdout.write('%s\n' % sshFail)
    sys.exit()
except socket.error, socketFail:
    sys.stdout.write('Failed to open socket\n')
    sys.stdout.write('%s\n' % socketFail)
    sys.exit()

并返回调试输出:

DEBUG:paramiko.transport:starting thread (client mode): 0x14511d0L
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_5.3)
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group14-sha1'] server key:['ssh-rsa'] client encrypt:['aes256-cbc', 'aes128-cbc', '3des-cbc'] server encrypt:['aes256-cbc', 'aes128-cbc', '3des-cbc'] client mac:['hmac-sha1'] server mac:['hmac-sha1'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
ERROR:paramiko.transport:Exception: Incompatible ssh peer (no acceptable kex algorithm)
ERROR:paramiko.transport:Traceback (most recent call last):
ERROR:paramiko.transport:  File "build\bdist.win32\egg\paramiko\transport.py", line 1546, in run
ERROR:paramiko.transport:    self._handler_table[ptype](self, m)
ERROR:paramiko.transport:  File "build\bdist.win32\egg\paramiko\transport.py", line 1618, in _negotiate_keys
ERROR:paramiko.transport:    self._parse_kex_init(m)
ERROR:paramiko.transport:  File "build\bdist.win32\egg\paramiko\transport.py", line 1731, in _parse_kex_init
ERROR:paramiko.transport:    raise SSHException('Incompatible ssh peer (no acceptable kex algorithm)')
ERROR:paramiko.transport:SSHException: Incompatible ssh peer (no acceptable kex algorithm)
ERROR:paramiko.transport:
Connection Failed!
Incompatible ssh peer (no acceptable kex algorithm)

我已确保我安装了最新版本的 pycrypto 和 paramiko。

【问题讨论】:

你是谁 DenverCoder9... 你看到了什么?! 对我来说,sudo easy_install paramiko 解决了这个问题。密钥交换 (kex) 算法可能是您可以更改的 sshd 设置? @BizNuge 如果您仍然遇到此问题,请查看以下来自 wisnia 的答案,该答案已为我修复。我编辑了帖子以使其更易于剪切和粘贴。 看起来像一个旧的 paramiko 错误。它正在等待合并github.com/paramiko/paramiko/pull/356 【参考方案1】:

该错误是在您的 paramiko 版本不支持使用您要连接的设备的密钥交换算法的情况下发生的。

ssh.connect('10.119.94.8', 22, username="user",password='passwor')
t = ssh.get_transport()
so = t.get_security_options()
so.kex
('diffie-hellman-group1-sha1', 'diffie-hellman-group-exchange-sha1')
so.ciphers
('aes128-ctr', 'aes256-ctr', 'aes128-cbc', 'blowfish-cbc', 'aes256-cbc', '3des-cbc', 'arcfour128', 'arcfour256')
paramiko.__version__
'1.10.1'

在 paramiko 日志中,您可以看到连接的密钥交换算法。

DEB paramiko.transport: starting thread (client mode): 0x11897150L
INF paramiko.transport: Connected (version 2.0, client OpenSSH_7.2)
DEB paramiko.transport: kex algos:['diffie-hellman-group14-sha1', 'ecdh-sha2-nistp256', 'ecdh-sha2-nistp384'] server key:['ssh-rsa'] client encrypt:['aes128-ctr', 'aes256-ctr'] server encrypt:['aes128-ctr', 'aes256-ctr'] client mac:['hmac-sha1'] server mac:['hmac-sha1'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
ERR paramiko.transport: Exception: Incompatible ssh peer (no acceptable kex algorithm)
ERR paramiko.transport: Traceback (most recent call last):
ERR paramiko.transport:     raise SSHException('Incompatible ssh peer (no acceptable kex algorithm)')
ERR paramiko.transport: SSHException: Incompatible ssh peer (no acceptable kex algorithm)

所以我建议升级到最新的 paramiko 版本,例如 2018 年的 2.4.2。在此版本中,密钥交换算法支持 sha1 和 sha2。

>>> ssh.connect("hostdev",22,username="user",password="pass")
>>> transport1=ssh.get_transport()
>>> so=transport1.get_security_options()
>>> so.kex
('ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1')
>>> 
>>> so.ciphers
('aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-cbc', 'aes192-cbc', 'aes256-cbc', 'blowfish-cbc', '3des-cbc')
>>> 
>>> print paramiko.__version__
2.4.2

【讨论】:

【参考方案2】:

如果其他人在使用 pip install paramiko --upgrade 升级后仍然遇到此问题,请确保您没有在系统范围内安装 paramiko,因为它将在 pip 之前加载,您可以使用 dpkg -l | grep paramiko 检查它,如果已安装,请将其删除并通过 pip 安装。

【讨论】:

【参考方案3】:

尝试使用 paramiko SSH 到 Aruba 设备时出现以下错误:

paramiko.ssh_exception.SSHException:不兼容的 ssh 对等体(没有可接受的 kex 算法)

进行 paramiko 升级解决了这个问题:

sudo pip install paramiko --upgrade

【讨论】:

【参考方案4】:

我升级了 paramiko 来解决这个问题:

 sudo pip install paramiko --upgrade

我的 paramiko 更新版本是:

paramiko==2.0.2

【讨论】:

我尝试时失败了。需要一些依赖项才能成功构建 paramiko 的依赖项。毕竟我仍然无法连接 - 我相信当前版本的 paramiko 对于我的系统(Ubuntu 14.04)来说太新并且不兼容。最后,我运行了sudo pip install paramiko == 1.16,这成功了。 @jdhildeb 谢谢,这有帮助,但我还必须卸载 python-paramiko 软件包系统软件包才能运行 sudo pip install paramiko==1.16【参考方案5】:

对我来说,我升级了 paramiko 的版本,它解决了问题。具体来说,我最初是通过 Ubuntu 14.04 python-paramiko 包安装 paramiko,并用最新的 pip (1.10 -> 1.16) 替换它。

【讨论】:

【参考方案6】:

这可能对 OP 的情况没有帮助,但希望它可以帮助遇到同样错误的其他人。

我遇到了这样一种情况,一个脚本可以通过 SSH 连接到系统,但另一个类似的脚本会失败

paramiko.SSHException: Incompatible ssh peer (no acceptable kex algorithm)

错误。

情况原来是我脚本顶部的 shebang 行:

#!/usr/bin/python

会失败,而

#!/usr/bin/env python

会成功的。

我在我的系统上使用 virtualenvs,所以失败的 /usr/bin/python 版本使用的是系统上安装的旧 Paramiko 版本,而 /usr/bin/env python 版本使用的是我的 virtualenv 中较新的 Paramiko 安装。

【讨论】:

【参考方案7】:

我在服务器端遇到了 Debian 8 和 OpenSSH 的类似问题。

作为快速修复,服务器端的以下 Cipher/MACs/KexAlgorithms 设置修复了该问题:

在 /etc/ssh/sshd_config 中:

Ciphers aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes128-ctr
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256,hmac-ripemd160,hmac-sha1
KexAlgorithms diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1

虽然...您应该从安全的角度分析这些设置。我是在实验室环境中设置的,所以没在意。

也不确定是否可以通过这种方式为 Cisco ACS 进行修改

【讨论】:

wisnia,谢谢你——即使在升级 paramiko 之后我也遇到了同样的问题,但使用你的设置(似乎足够安全)解决了我的问题。 你能解释一下你在这里做什么吗? 对 /etc/ssh/sshd_config 的最低要求是:“KexAlgorithms diffie-hellman-group-exchange-sha1”。我真的使用了上面的列表并开始剥离。然而,从安全角度来看,该算法并不是最安全的,正如原帖中提到的那样。 谢谢兄弟们!此解决方案可用于 ubuntu、centos 和 fedora。我花了一整天的时间

以上是关于paramiko 不兼容的 ssh 对等体(没有可接受的 kex 算法)的主要内容,如果未能解决你的问题,请参考以下文章

安卓wifi直连。设置 PBC 消息和对等体列表

paramiko 几种不方式SSH与FTP登录连接方式

SSH 窗口大小如何影响 paramiko

删除多个 vnet 对等体 azure CLI

在 Paramiko 中运行交互式命令

@angular/cdk@5.0.3 需要 @angular/common@~5.1.1 的对等体,但没有安装