Paramiko 使用来自 Pageant 的密钥进行身份验证时出现“UnicodeDecodeError”

问题描述 投票:0回答:1

尝试使用 Paramiko 和 SSH 代理 Pageant 连接到服务器时遇到问题。该错误表明脚本在连接尝试期间失败,特别是当 Paramiko 尝试与 SSH 代理 (Pageant) 交互并处理关键数据时。错误消息表明 Paramiko 在需要 UTF-8 编码数据时遇到了非 UTF-8 字节序列:

“utf-8'编解码器无法解码位置 1 中的字节 0x82:无效起始字节”

但是,解码并不能解决问题。

Traceback (most recent call last):
File: “c:../ssh_little.py”, line 11, in module,
    client.connect(hostname=hostname, username=username)
File “c:/..../conda/Lib/site-package/paramiko/agent.py, line 415, in __init__ self.connect(conn)
File “c:/..../conda/Lib/site-package/paramiko/agent.py, line 89, in _connect AgentKey(
File “c:/..../conda/Lib/site-package/paramiko/agent.py, line 443, in __init__ self.name = msg.get_text()
File “c:/..../conda/Lib/site-package/paramiko/message.py, line 184, in get_text return u(self.get_string())
File “c:/..../conda/Lib/site-package/paramiko/util.py, line 333, in u return s.decode(encoding)
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x82 in position1: invalid start byte

这是使用的代码:

import paramiko

hostname = 'host.com'
username = 'user'

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=hostname, username=username)

另一方面,我可以使用 PuTTY 和 Pageant 使用相同的密钥手动连接到该服务器。

请问有什么想法吗?谢谢!

python ssh paramiko ssh-keys pageant
1个回答
1
投票

在 macOS 上,我可以在私钥注释中使用

ssh-agent
字符重现相同的异常(使用
ssh-add
\x82
):

$ ssh-add -l
1024 SHA256:DFZU0tLv5WaWOkqqOrMQSF/hSyYxU+rYnv8sP4OU2P0 foobar (RSA)
$ ssh-add -l | hexdump -C
00000000  31 30 32 34 20 53 48 41  32 35 36 3a 44 46 5a 55  |1024 SHA256:DFZU|
00000010  30 74 4c 76 35 57 61 57  4f 6b 71 71 4f 72 4d 51  |0tLv5WaWOkqqOrMQ|
00000020  53 46 2f 68 53 79 59 78  55 2b 72 59 6e 76 38 73  |SF/hSyYxU+rYnv8s|
00000030  50 34 4f 55 32 50 30 20  66 6f 6f 82 62 61 72 20  |P4OU2P0 foo.bar |
00000040  28 52 53 41 29 0a                                 |(RSA).|
00000046
$ python3 ssh_little.py
Traceback (most recent call last):
  File "/private/tmp/ssh_little.py", line 9, in <module>
    client.connect(hostname=hostname, username=username, port=port)
  File "/usr/local/lib/python3.11/site-packages/paramiko/client.py", line 485, in connect
    self._auth(
  File "/usr/local/lib/python3.11/site-packages/paramiko/client.py", line 745, in _auth
    self._agent = Agent()
                  ^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/paramiko/agent.py", line 415, in __init__
    self._connect(conn)
  File "/usr/local/lib/python3.11/site-packages/paramiko/agent.py", line 92, in _connect
    comment=result.get_text(),
            ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/paramiko/message.py", line 184, in get_text
    return u(self.get_string())
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/paramiko/util.py", line 333, in u
    return s.decode(encoding)
           ^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x82 in position 3: invalid start byte

在上面的

hexdump
输出中,我们可以看到
\x82
foo
之间有一个
bar


在 Windows 上我有一个 PuTTY

.ppk
(我假设你的私钥是由 PuTTY 生成的,因为你使用的是 Pageant) 内容如下:

$ cat rsa.ppk
PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: putty-keygen-pageant-2020-07-01
... ...
... ...

这里有一个

Comment:
部分。因此,请检查您的私钥文件,看看是否有非 UTF8 字符。

© www.soinside.com 2019 - 2024. All rights reserved.