python3的fabric3无法免密登录

报错:paramiko.ssh_exception.AuthenticationException: Authentication failed

处理fabric3免密登录的问题

1.问题

今天使用python3的fabric3要免密登录主机A,结果报错:

paramiko.ssh_exception.AuthenticationException: Authentication failed.

而ssh命令行是可以直接免密的。

2.定位和解决

因为底层是使用paramiko,我们先使用这个库来直接连接。

import paramiko

# 发送 paramiko 日志到 syslogin.log 文件
paramiko.util.log_to_file('syslogin.log')   
# 配置私人密钥文件位置
private = paramiko.RSAKey.from_private_key_file('/home/testerzhang/.ssh/id_rsa')

# 实例化 SSHClient
client = paramiko.SSHClient()

# 自动添加策略,保存服务器的主机名和密钥信息,如果不添加,那么不在本地 know_hosts 文件中记录的主机将无法连接
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

# 连接 SSH 服务端,以用户名和密码进行认证
client.connect(hostname='10.10.10', port=22022, username='lab', pkey=private)

查看下syslogin.log日志:

DEB [20220107-12:54:51.413] thr=1   paramiko.transport: Our pubkey algorithm list: ['rsa-sha2-512', 'rsa-sha2-256', 'ssh-rsa']
DEB [20220107-12:54:51.413] thr=1   paramiko.transport: Server-side algorithm list: ['']
DEB [20220107-12:54:51.413] thr=1   paramiko.transport: Agreed upon 'rsa-sha2-512' pubkey algorithm
INF [20220107-12:54:51.423] thr=1   paramiko.transport: Authentication (publickey) failed.

居然不支持rsa-sha2-512

我们看下paramiko库版本:

$ pip3 list|grep paramiko
paramiko              2.9.1     

于是去翻了一下这个版本说明: image.png

好吧,那么我们降低下版本:

$ pip3 install paramiko==2.7.2

再次运行,成功,搞定!


本文没有授权给任何组织、企业和个人转载,未经作者允许禁止转载!

欢迎关注我的公众号testerzhang,原创技术文章第一时间推送。

公众号二维码

updatedupdated2022-03-142022-03-14