尝试了很多次,使用正确的私钥在python2下能执行,但是在python3下总是报如下错误,认证失败
paramiko.ssh_exception.AuthenticationException: Authentication failed.
import paramiko
# 读取本地当前文件夹下的私钥
private_key = paramiko.RSAKey.from_private_key_file('id_rsa')
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='192.168.168.168', port=22, username='root', pkey=private_key,
disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']})
# 执行命令
stdin, stdout, stderr = ssh.exec_command('pwd')
# 获取命令结果
result = stdout.read()
print(result.decode('utf-8'))
# 关闭连接
ssh.close()
由于在密钥交换过程中,服务器主机密钥的客户端验证现在将优先使用rsa-sha2-512、rsa-sha2-256和传统的ssh-rsa算法,而不仅仅是ssh-rsa。您的目标系统既不支持RSA2,也不支持server-sig-algs协议扩展,则需要采取相应的操作,禁用掉
disabled_algorithms={‘pubkeys’: [‘rsa-sha2-256’, ‘rsa-sha2-512’]}
重点在于上述代码禁用掉上述算法,具体原因参考文档:pramiko文档