我正在尝试使用以下代码建立到跳转服务器的 SHH 隧道:
import sshtunnel
server = sshtunnel.SSHTunnelForwarder(
('jump host', jump_port),
ssh_username = 'my_user_name',
ssh_pkey = '~/.ssh/id_rsa',
ssh_private_key_password = 'my private key pwd',
remote_bind_address = ('remote_host', remote_port),
local_bind_address = ('localhost', local_port)
)
但是反复出现错误:
| ERROR | Password is required for key C:\Users\<my_local_user_name>/.ssh\id_rsa
我检查了几个类似的问题并尝试了建议的解决方案,但还没有成功。
我确信我的 RSA 密钥文件和密码都是正确的。 我已经用以下代码检查过:
import paramiko
pkey='C:/Users/<my_local_user_name>/.ssh/id_rsa'
key=paramiko.RSAKey.from_private_key_file(pkey, 'my private key pwd')
它可以工作并成功创建
key
对象。
我还检查了
sshtunnel
的代码,并找到了触发此类错误消息的唯一位置:
据我了解,在 1304 行中对
pkey_class.from_private_key_file
的调用应该可以很好地工作,就像我在上面直接调用 paramiko.RSAKey.from_private_key_file
一样,但由于某些原因,它会在这里触发 PasswordRequiredException
。
我还检查了paramiko源代码,发现只有当密钥文件加密且密码为None时才会引发此异常,我认为这不是我的情况,因为我正确指定了密码。
通过
allow_agent = True
也没有帮助。
将
ssh_pkey
作为 paramiko.RSAKey
对象传递也没有帮助。
所以现在我完全迷失了...
有什么想法吗?
*PS:我通过 Jupiter Notebook 使用 Python 3。
PPS:跳转服务器和公钥文件都正常,因为我使用 DBeaver 成功连接到它*
您使用的库似乎存在问题:https://pypi.org/project/sshtunnel/
您是否尝试仅将其用作 CLI 工具? 可以检查 ssh_key + 密码,如果这有效,那么您就有解决方案了。
如果问题仅与您当前的代码设置有关,我建议打开一个问题和库 github 位置。 也许作者很快就会提供修复程序?
问候。