问题:出现paramiko.ssh_exception.AuthenticationException: Authentication failed.

描述:远程主机执行 SSH 命令。它工作正常,但今天我遇到了一个奇怪的错误,对远程服务器(基于 RHEL 6.5 的服务器)执行命令出现paramiko.ssh_exception.AuthenticationException: Authentication failed

排查方法:

ssh可以进去,说明本地密码或密钥正常,无需在指定,查看远程日志发现/var/log/secure 中出现

userauth_pubkey: unsupported public key algorithm: rsa-sha2-512
userauth_pubkey: unsupported public key algorithm: rsa-sha2-512

调试日志发现:

DEBUG:paramiko.transport:Finalizing pubkey algorithm for key of type 'ssh-rsa'
DEBUG:paramiko.transport:Our pubkey algorithm list: ['rsa-sha2-512', 'rsa-sha2-256', 'ssh-rsa']
DEBUG:paramiko.transport:Server did not send a server-sig-algs list; defaulting to our first preferred algo ('rsa-sha2-512')
DEBUG:paramiko.transport:NOTE: you may use the 'disabled_algorithms' SSHClient/Transport init kwarg to disable that or other algorithms if your server does not support them!
INFO:paramiko.transport:Authentication (publickey) failed.

这是 Paramiko 中的一个错误。它不能正确处理 server-sig-algs 的缺失服务器端的扩展。

尝试禁用 rsa-sha2-*完全在 Paramiko 方面:

ssh_client.connect(
  server, username=ssh_user,disabled_algorithms=dict(pubkeys=["rsa-sha2-512", "rsa-sha2-256"]))