项目中使用了paramiko作为ssh客户端去连接Linux服务器,之前使用一直正常,今天突然报错了,报错信息如下:
Exception(client): Unable to agree on a pubkey algorithm for signign a 'ssh-rsa' key!
Traceback
...
paramiko.ssh_exception.SSHException:No existing session
我的示例代码如下, paramiko版本是3.1.0
import paramiko
ip = 'xxx.xxx.xxx.xxx'
port = 22
user = 'xxx'
password = 'xxx'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, port=port, username=user, password=password)
OpenSSH_6.6.1p1, OpenSSL 1.0.1e-fips 11 # 可正常连接
OpenSSH_8.3p1, without OpenSSL #连接报错
网上查阅了大量资料,其中大部分都是在connect方法中添加参数,如下:
ssh.connect(ip, port,user,password, disabled_algorithms={'keys': ['rsa-sha2-256', 'rsa-sha2-512']})
或
ssh.connect(ip, port,user,password, disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']})
奈何都不能够解决问题,该报的错一点没少,
最终通过我不断的尝试,使用下面的方式解决了问题,大家拿走不谢~
import paramiko
ip = 'xxx.xxx.xxx.xxx'
port = 22
user = 'xxx'
password = 'xxx'
transport = paramiko.Transport((ip, port))
transport.connect(username=user, password=password)
ssh = paramiko.SSHClient()
ssh._transport = transport
stdin, stdout, stderr = ssh.exec_command('pwd')
print(stdout.read().decode('utf8'))