相关文章推荐
买醉的香蕉  ·  SHA ...·  3 年前    · 
有情有义的蚂蚁  ·  DevExpress WinForm ...·  3 年前    · 
帅气的牛肉面  ·  sql - Error 1046 No ...·  3 年前    · 

Paramiko AuthenticationException问题

17 人关注

我在用Paramiko(1.7.6-2版)ssh客户端连接设备时遇到了问题。

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect("123.0.0.1", username="root", password=None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 327, in connect
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
  File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 481, in _auth
    raise saved_exception
paramiko.AuthenticationException: Authentication failed.

当我从命令行使用ssh时,它工作得很好。

ssh root@123.0.0.1
BusyBox v1.12.1 (2010-11-03 13:18:46 EDT) built-in shell (ash)
Enter 'help' for a list of built-in commands.

有谁见过这个吗?

Edit 1

下面是ssh命令的粗略输出。

:~$ ssh -v root@123.0.0.1
OpenSSH_5.3p1 Debian-3ubuntu4, OpenSSL 0.9.8k 25 Mar 2009
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 123.0.0.1 [123.0.0.1] port 22.
debug1: Connection established.
debug1: identity file /home/waffleman/.ssh/identity type -1
debug1: identity file /home/waffleman/.ssh/id_rsa type -1
debug1: identity file /home/waffleman/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.1
debug1: match: OpenSSH_5.1 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3p1 Debian-3ubuntu4
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host '123.0.0.1' is known and matches the RSA host key.
debug1: Found key in /home/waffleman/.ssh/known_hosts:3
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentication succeeded (none).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.utf8

Edit 2 Here is the python output with debug output:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko, os
>>> paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
>>> ssh = paramiko.SSHClient()
>>> ssh.load_system_host_keys()
>>> ssh.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect("123.0.0.1", username='root', password=None)
DEBUG:paramiko.transport:starting thread (client mode): 0x928756cL
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_5.1)
DEBUG:paramiko.transport:kex algos:['diffie-hellman-group-exchange-sha256', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'ssh-dss'] client encrypt:['aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'arcfour128', 'arcfour256', 'arcfour', 'aes192-cbc', 'aes256-cbc', 'rijndael-cbc@lysator.liu.se', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr'] server encrypt:['aes128-cbc', '3des-cbc', 'blowfish-cbc', 'cast128-cbc', 'arcfour128', 'arcfour256', 'arcfour', 'aes192-cbc', 'aes256-cbc', 'rijndael-cbc@lysator.liu.se', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr'] client mac:['hmac-md5', 'hmac-sha1', 'umac-64@openssh.com', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] server mac:['hmac-md5', 'hmac-sha1', 'umac-64@openssh.com', 'hmac-ripemd160', 'hmac-ripemd160@openssh.com', 'hmac-sha1-96', 'hmac-md5-96'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEBUG:paramiko.transport:Ciphers agreed: local=aes128-ctr, remote=aes128-ctr
DEBUG:paramiko.transport:using kex diffie-hellman-group1-sha1; server key type ssh-rsa; cipher: local aes128-ctr, remote aes128-ctr; mac: local hmac-sha1, remote hmac-sha1; compression: local none, remote none
DEBUG:paramiko.transport:Switch to new keys ...
DEBUG:paramiko.transport:Trying discovered key b945197b1de1207d9aa0663f01888c3c in /home/waffleman/.ssh/id_rsa
DEBUG:paramiko.transport:userauth is OK
INFO:paramiko.transport:Authentication (publickey) failed.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 327, in connect
    self._auth(username, password, pkey, key_filenames, allow_agent, look_for_keys)
  File "/usr/lib/pymodules/python2.6/paramiko/client.py", line 481, in _auth
    raise saved_exception
paramiko.AuthenticationException: Authentication failed.
    
4 个评论
我试着用它来连接运行嵌入式Linux的QNAP TS-409。工作很顺利。
I guess you could do ssh root@123.0.0.1 to see how ssh is connecting.
做 "ssh -v root@123.0.0.1",看看SSH是使用公钥认证还是其他认证方法?
Seems that this is the answer for you --> stackoverflow.com/questions/14274566/...
python
ssh
paramiko
waffleman
waffleman
发布于 2010-11-09
9 个回答
JimB
JimB
发布于 2022-02-15
已采纳
0 人赞同

远程设备上的ssh服务器拒绝了你的认证。确保你使用的是正确的密钥,公钥存在于 authorized_keys 中, .ssh 目录权限正确, authorized_keys 权限正确,并且设备没有任何其他访问限制。没有服务器的日志,很难说发生了什么。

[编辑]我刚刚回看了你的输出,你正在使用 None 认证。这通常是不允许的,它被用来确定服务器允许哪些认证方法。有可能你的服务器使用的是基于主机的认证(或者根本就没有!)。

由于 auth_none() 很少使用,它不能从 SSHClient 类中访问,所以你需要直接使用 Transport

transport.auth_none('root') 
    
嗨 @waffleman , 我也遇到了同样的问题......所以我在代码中加入了transport.auh_none('root')一行,但现在得到的错误是......NameError: name 'transport' is not defined
@Spike:你声明了运输吗? transport = ssh.get_transport()
@JimB- 接得好。我的主机在没有密码的情况下允许我从命令行进入,即使我使用了 ssh -i $HOME/.ssh/id_rsa command ,但当我注意到我实际上没有使用钥匙时,我才能够解决这个问题。 ssh -v 说, debug1: Authentication succeeded (gssapi-with-mic). (这是AD,我相信)。我把我的authorized_keys文件复制到主机上,paramiko就可以工作了。
realcp1018
realcp1018
发布于 2022-02-15
0 人赞同

这确实是个老问题了,但我刚刚遇到了同样的错误,我想列出以下信息会有帮助。

  • I'm using paramiko 2.9.1 and python>=3.6, make sure your paramiko>=2.9.0
  • cmd ssh <hostname> works fine
  • Code below get error: AuthenticationException: Authentication failed.
  • import paramiko
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    session = client.connect("<hostname>")
    

    From here https://github.com/paramiko/paramiko/issues/1984,我知道这是一个与认证算法有关的错误。

    你需要在connect()中添加一个disabled_algorithms参数,见这里的文档。https://www.paramiko.org/changelog.html#2.9.0

    但2.9.0的更新日志中,disabled_algorithms有一个错字,应该是。

    client.connect("<hostname>", disabled_algorithms={'pubkeys': ['rsa-sha2-256', 'rsa-sha2-512']})
    
    client.connect("<hostname>", disabled_algorithms={'keys': ['rsa-sha2-256', 'rsa-sha2-512']})
    

    最后,一切都很顺利。

    刚刚收到其他主机的错误Unable to agree on a pubkey algorithm for signing a 'ssh-rsa' key!,可能把paramiko降级到2.9.0以下是一个更好的方法。

    非常感谢! 我一直在想,为什么我的Paramiko代码在Linux上可以工作,但在Mac上却不行。 添加你上面给出的 "disabled_algorithms "值后,我就解决了这个问题。 呼......我在谷歌上搜索了好几遍。 很高兴在我放弃之前遇到了这个问题。
    user5417363
    user5417363
    发布于 2022-02-15
    0 人赞同

    作为这个问题的一个非常晚的后续行动,我相信我遇到了与waffleman相同的问题,在一个封闭的网络环境中。

    关于在 Transport 对象上使用 auth_none 的提示原来是很有帮助的,但我发现自己对如何实现这一点有些不解。问题是,至少到今天为止,我无法得到一个 Transport 对象的 SSHClient 对象,直到它连接起来;但它首先不会连接......

    因此,如果这对其他人有用,我的工作方法如下。我只是覆盖了 _auth 的方法。

    好吧,这是很脆弱的,因为 _auth 是一个私人的东西。我的其他选择是--实际上仍然是--手动创建 Transport Channel 对象,但就目前而言,我觉得我在引擎盖下的所有这些东西要好得多。

    from paramiko import SSHClient, BadAuthenticationType
    class SSHClient_try_noauth(SSHClient):
        def _auth(self, username, *args):
                self._transport.auth_none(username)
            except BadAuthenticationType:
                super()._auth(username, *args)
        
    David Johnson
    David Johnson
    发布于 2022-02-15
    0 人赞同

    确保公钥和私钥文件(可能还有包含的文件夹)的权限被设置为非常严格(即chmod 600 id_rsa)。事实证明,这是(操作系统)要求的,以便将这些文件用作ssh钥匙。这是我的好同事告诉我的 :) 还要确保你在给定的ssh钥匙上使用的是正确的用户名。

    SilentGhost
    SilentGhost
    发布于 2022-02-15
    0 人赞同

    paramiko的SSHClient load_system_host_keys 方法,你可以用它来加载用户特定的密钥集。正如文档中的例子所解释的,它需要在连接到服务器之前运行。

    运气不好。我还试着删除了 known_hosts,看看是否有什么效果。
    anibal
    anibal
    发布于 2022-02-15
    0 人赞同

    我得到了类似的错误,当服务器使用AD认证时。我想这是paramiko的一个错误。我已经知道,在使用paramiko之前,我必须先设置ssh密钥。

    Tagar
    Tagar
    发布于 2022-02-15
    0 人赞同

    可能有不同的原因在 server (你正在连接的sshd),所以在客户端可能很难调试。

    例如, tail -f /var/log/secure

    10月9日 15:50:26 pc1udatahgw04 sshd[27501]。拒绝认证:bad 目录/home/testuser的所有权或模式。

    如果你运行 ls -lad /home/testuser 来查看权限,你会看到例如在我们的案例中。

    $ ls -lad /home/testuser
    drwxrwxr-x 16 testuser  testgroup 57344 Oct  9 15:23 /home/testuser
    

    注意第二个w位。主目录被开放给群组写入。在这种情况下,sshd拒绝了基于密钥的认证。

    再次,检查sshd的日志server侧面。可能还有其他问题,如已经提到的

  • /home/user/.ssh directory is too open
  • /home/user/.ssh/id_rsa file is too open
  • /home/user/.ssh/id_rsa.pub file is too open
  • /home/user/.ssh/id_ecdsa file is too open
  • /home/user/.ssh/id_ecdsa.pub file is too open
  • etc..

    questionto42standswithUkraine
    questionto42standswithUkraine
    发布于 2022-02-15
    0 人赞同

    venv installation also makes global files

    在一个venv中安装paramiko,会在venv和全局环境中安装文件。仅在该venv中使用paramiko似乎并不奏效。

    在codium / vscode中,要在一个不能访问venv的文件夹中,然后在基本环境中使用paramiko。如果你从venv中卸载它,基础环境就不会再运行paramiko。

    从所有这些看来,最好是安装paramiko only 在基本环境中,这样它对任何venv也是可用的。

    Details

    installation in the venv leads to global files as well

    在我的例子中,只有当我在一个虚拟环境(venv)中,或者当我在一个也包含venv的文件夹中,但激活了基本环境的Python解释器时,这个错误才会弹出。

    >>> ssh.connect(host, port=port, username=user, key_filename=key_filepath)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/MY_USER/Documents/MY_PROJECT/MY_VENV/lib/python3.8/site-packages/paramiko/client.py", line 435, in connect
        self._auth(
      File "/home/MY_USER/Documents/MY_PROJECT/MY_VENV/lib/python3.8/site-packages/paramiko/client.py", line 766, in _auth
        raise saved_exception
      File "/home/MY_USER/Documents/MY_PROJECT/MY_VENV/lib/python3.8/site-packages/paramiko/client.py", line 742, in _auth
        self._transport.auth_publickey(username, key)
      File "/home/MY_USER/Documents/MY_PROJECT/MY_VENV/lib/python3.8/site-packages/paramiko/transport.py", line 1634, in auth_publickey
        return self.auth_handler.wait_for_response(my_event)
      File "/home/MY_USER/Documents/MY_PROJECT/MY_VENV/lib/python3.8/site-packages/paramiko/auth_handler.py", line 258, in wait_for_response
        raise e
    paramiko.ssh_exception.AuthenticationException: Authentication failed.
    

    只有当我在代码编辑器中加载任何文件夹作为项目文件夹时,下面的脚本才起作用,该文件夹中没有安装有Paramiko的venv。

    from os import getenv
    import paramiko
    from dotenv import load_dotenv
    load_dotenv(MY_FULL_PATH, override=True)
    ssh = paramiko.SSHClient()
    # ssh.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    host = getenv("MY_HOST")
    port = getenv("MY_PORT")
    user = getenv("MY_USER")
    key_filepath = getenv("MY_SSH_KEY_FILEPATH")
    ssh.connect(host, port=port, username=user, key_filename=key_filepath)
    sftp = ssh.open_sftp()
    sftp.put(MY_FILEPATH1, MY_FILEPATH2)
    

    只要项目文件夹中存在一个安装了Paramiko的venv,Paramiko似乎就会默认使用该venv,并弹出该错误。即使你选择基本环境作为解释器而不是。

    我只能猜测,这是一个当Paramiko同时安装在基本环境和venv中时发生的问题,就像我的情况一样,尽管我安装了它only in the venv.

    uninstall from the base env

    当我试图从基础环境中卸载它时,它没有找到任何文件。

    pip3 uninstall paramiko
    Found existing installation: paramiko 2.6.0
    Not uninstalling paramiko at /usr/lib/python3/dist-packages, outside environment /usr
    Can't uninstall 'paramiko'. No files were found to uninstall.
    

    不过,在搜索grep -lR paramiko /usr时,我在./lib/python3/dist-packages/找到了它。而且我在两个venv中也有它。我的猜测是,Paramiko不能处理在venv中的安装,因为当你不在venv中时,它仍然被成功使用。如果你在一个可以进入实际安装了它的venv的文件夹中,它就不能工作,除非你再次卸载它(已测试)。导致错误的venv是一个全新的设置,因为我在另一个现有venv中安装Paramiko时遇到了问题。解决办法是将其从venv中卸载,然后我可以使用venv并从全局安装中获得Paramiko,可能是因为全局安装被venv安装所支配,然后又错误地与全局安装交织在一起。

    uninstall from the venv

    当我从venv中卸载它时,在基本环境中再也找不到paramiko了。