从内存创建paramiko RSA公钥实例



我正在使用paramiko==2.11.0,并试图创建内存中公钥的RSAKey表示,因为我稍后会将其添加为已知的主机密钥。

import paramiko
host_public_key_data = b"""
---- BEGIN SSH2 PUBLIC KEY ----
Comment: ""
AAAAB3NzaC1yc2EAAAADAQABAAACAQCHr4+70xMW2KkShW0/+VAZQQ/dMgGPcKRS
zOWhkSgvAuKPTpxkmkMh5/tXHKkFuTWdrGF4hP0acBIzkYTubpwJO3q4aqnSCD1C
xpMa8uZXgTHKAx3xUkBlZGUYrNSqjD4+yNIV/mkApzAnjGlupLxwH+pfKIY6IfbA
B2HtD3RbvUCWGKbPORRZKCuDLmztfDajM2l8eyixWn2/iwbI00usuUnvMbKsaN9t
NNjHz06dQITWWNuQICwBNiyWiWpVPezdxkXZ2MkfbdIKdFK72MAtXG4CFvk5x9Jl
7xjb53Yv5Dv94HmCXR43WQ8+WB2iBhUWt08SWr9qG3CAir4GT5D3l3esdV5mIKZ+
7T9TtATXa8HJE8EcB4nAblKXnAFSbzIL/qvCiEJkk2GfwUDWzll21o4HljZWHK0T
odwReL7aQb44XdTB26CMGnDxLhCImbRYeFrolm6Q/K8yAm6QSiZdFYXolQ7wj1fU
aTwzCea+EY1nEj/Uq77l4w7zU6YaQtiotEtVqb/4x5YRyJI/JiPDA56k+gXB+Rm6
jBS+qOL+dP/LeTQNt3d1nz4jw6E0MkXqSIk8drd/TOnIslkBJtm2YpBDaBvRLg2b
EXMDBCtTnjBA/Xnk7LnRFUcq1PjI/4kckNlhRgIbj0d9S6RzFPHjq9i0iCCX47ws
eI7DoRWLXw==
"""
host_public_key = paramiko.RSAKey(data=host_public_key_data) # public key used as host key
ssh_client = paramiko.SSHClient()
ssh_client.get_host_keys().add(HOST, 'ssh-rsa', host_public_key)
ssh_client.connect(HOST, port=PORT, pkey=my_client_private_key)

然而,我得到了以下错误:

SSHException: Invalid key (class: RSAKey, data type: - BEGIN SSH2 PUBLIC KEY ----
Comment: ""
AAAAB3NzaC1yc2EAAAADAQABAAACAQCHr4+70xMW2KkShW0/+VAZQQ/dMgGPcKRS
zOWhkSgvAuKPTpxkmkMh5/tXHKkFuTWdrGF4hP0acBIzkYTubpwJO3q4aqnSCD1C
xpMa8uZXgTHKAx3xUkBlZGUYrNSqjD4+yNIV/mkApzAnjGlupLxwH+pfKIY6IfbA
B2HtD3RbvUCWGKbPORRZKCuDLmztfDajM2l8eyixWn2/iwbI00usuUnvMbKsaN9t
NNjHz06dQITWWNuQICwBNiyWiWpVPezdxkXZ2MkfbdIKdFK72MAtXG4CFvk5x9Jl
7xjb53Yv5Dv94HmCXR43WQ8+WB2iBhUWt08SWr9qG3CAir4GT5D3l3esdV5mIKZ+
7T9TtATXa8HJE8EcB4nAblKXnAFSbzIL/qvCiEJkk2GfwUDWzll21o4HljZWHK0T
odwReL7aQb44XdTB26CMGnDxLhCImbRYeFrolm6Q/K8yAm6QSiZdFYXolQ7wj1fU
aTwzCea+EY1nEj/Uq77l4w7zU6YaQtiotEtVqb/4x5YRyJI/JiPDA56k+gXB+Rm6
jBS+qOL+dP/LeTQNt3d1nz4jw6E0MkXqSIk8drd/TOnIslkBJtm2YpBDaBvRLg2b
EXMDBCtTnjBA/Xnk7LnRFUcq1PjI/4kckNlhRgIbj0d9S6RzFPHjq9i0iCCX47ws
eI7DoRWLXw==
---- END SSH2 PUBLIC KEY ----

知道帕拉米科为什么要把它声明为无效密钥吗?

我认为您尝试使用的RSAKey类不支持公钥。经过一点搜索和测试,似乎可以将公钥类用于公钥。不确定这是否仍然适用于您的场景

https://docs.paramiko.org/en/stable/api/keys.html#paramiko.pkey.PKey

相关内容

  • 没有找到相关文章

最新更新