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

29

使用paramiko库尝试SSH连接到Cisco ACS设备时,我遇到了以下错误。我以前在Python中使用paramiko没有问题,并且我可以从命令行或使用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。


7
你是 DenverCoder9,你看到了什么? - BizNuge
对我来说,“sudo easy_install paramiko”解决了这个问题。密钥交换(kex)算法可能是一个可以更改的sshd设置? - Justin
2
@BizNuge如果你仍然遇到这个问题,请检查下面wisnia的答案,这为我解决了问题。我编辑了帖子以便更容易剪切和粘贴。 - Peter Grace
看起来像是一个旧的paramiko bug。它正在等待合并 https://github.com/paramiko/paramiko/pull/356 - Samuel
8个回答

21

我在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,仍然遇到了同样的问题,但使用您的设置(看起来足够安全)解决了我的问题。 - Peter Grace
你能解释一下你在这里具体做什么吗? - Daan
3
需要添加到/etc/ssh/sshd_config的最小内容是:"KexAlgorithms diffie-hellman-group-exchange-sha1"。我真的使用了上面的列表并开始剥离。然而,从安全角度来看,该算法并不是最安全的,正如原帖中所提到的。 - Sandeep Singhal
谢谢兄弟们!这个解决方案适用于Ubuntu、CentOS和Fedora。我花了一整天来完成它。 - Toandd

20

我升级了paramiko以解决问题:

 sudo pip install paramiko --upgrade

我的更新版本的paramiko是:

paramiko==2.0.2


3
我尝试后失败了。为了成功构建paramiko的依赖项,需要一些其他模块。即使这样做了,我仍然无法连接 - 我认为当前版本的paramiko对我的系统(Ubuntu 14.04)太新,不兼容。最终,我运行了 sudo pip install paramiko == 1.16,这成功了。 - jdhildeb
@jdhildeb 谢谢,这很有帮助,但我还必须卸载python-paramiko系统包才能运行sudo pip install paramiko==1.16 - Caumons

2
在使用paramiko尝试ssh到Aruba设备时,我遇到了以下错误: paramiko.ssh_exception.SSHException: Incompatible ssh peer (no acceptable kex algorithm)
通过升级paramiko版本解决了这个问题:
sudo pip install paramiko --upgrade

2

如果你在使用 pip install paramiko --upgrade 升级后仍然遇到此问题,请确保你没有在系统范围内安装 paramiko,因为它会在 pip 安装的版本之前被加载。你可以通过 dpkg -l | grep paramiko 进行检查。如果已经安装了,请卸载并通过 pip 重新安装。


1

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


0

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

我遇到了这样一种情况:一个脚本可以成功地SSH进入系统,但另一个类似的脚本却会出现相同的错误。

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

错误。

问题出在我的脚本顶部的shebang行:

#!/usr/bin/python

会失败,而

#!/usr/bin/env python

会成功。

我在我的系统上使用虚拟环境,所以失败的/usr/bin/python版本使用了系统上安装的较旧的Paramiko版本,而/usr/bin/env python版本则使用了我虚拟环境中安装的更新的Paramiko版本。


0

最近我更新了我的服务器从Ubuntu 20到22,并更换了不同的VPS提供商,发现了这个问题。手动SSH没有问题,什么也没变,但是paramiko却破坏了我的脚本。

在本地,我的Python 3.8虚拟环境中有:

paramiko 2.8.1

在通常的连接调用时:

from paramiko import SSHClient
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(...

我收到了以下信息: paramiko.ssh_exception.SSHException: 不兼容的ssh对等体(没有可接受的主机密钥) 正如 Romaan早些时候所说,我所需要的就是:
pip install --upgrade paramiko
...
Successfully installed paramiko-2.11.0

只是想用我的背景说明他的有用回答仍然相关。


0

这个错误是因为你的 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接