如何使用 SSH 密钥通过 Paramiko 连接到 SFTP - Pageant [英] How to connect to SFTP through Paramiko with SSH key - Pageant

查看:75
本文介绍了如何使用 SSH 密钥通过 Paramiko 连接到 SFTP - Pageant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用受密码保护的 SSH 密钥通过 Paramiko 连接到 SFTP.我已将密钥加载到 Pageant 中(据我所知,Paramiko 支持它),但我无法用它来解密我的私钥.

I am trying to connect to an SFTP through Paramiko with a passphrase protected SSH key. I have loaded the key into Pageant (which I understand is supported by Paramiko) but I can't get it to decrypt my private key.

我在这里找到了这个例子,它引用了allow_agent=True 但这似乎不是可以与 SFTPClient 一起使用的参数.

I have found this example here that references allow_agent=True but this does not appear to be a parameter that can be used with the SFTPClient.

谁能告诉我是否可以以这种方式与 Paramiko 和 Pageant 合作?

Can anyone advise if it is possible to work with Paramiko and Pageant in this way?

这是我目前的代码 - 引发 PasswordRequiredException

This is my code at the moment - which raises PasswordRequiredException

privatekeyfile = 'path to key'
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
transport = paramiko.Transport(('host', 'port'))
transport.connect('username',pkey = mykey)
sftp = paramiko.SFTPClient.from_transport(transport)

推荐答案

当使用 RSAKey.from_private_key_file.

但请注意,在使用 Pageant 时,您不必完全加载密钥.这就是使用身份验证代理的意义所在.但只有 SSHClient 支持盛会.Transport 类本身没有.

Though note that you do not have to load the key at all, when using the Pageant. That's the point of using an authentication agent. But only the SSHClient class supports the Pageant. The Transport class does not, on its own.

您可以按照如何在 Windows 上通过 Paramiko 使用 Pageant 中的代码?
尽管 allow_agent 默认为 True,但实际上代码没有什么特别的.

You can follow the code in How to use Pageant with Paramiko on Windows?
Though as the allow_agent is True by default, there is actually nothing special about the code.

一旦连接并通过身份验证,请使用 SSHClient.open_sftp 方法 获取SFTPClient 的实例.

Once connected and authenticated, use the SSHClient.open_sftp method to get your instance of the SFTPClient.

ssh = paramiko.SSHClient()
ssh.connect(host, username='user', allow_agent=True)
sftp = ssh.open_sftp()

您还需要验证主机密钥:
Paramiko未知服务器"

You will also need to verify the host key:
Paramiko "Unknown Server"

这篇关于如何使用 SSH 密钥通过 Paramiko 连接到 SFTP - Pageant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆