python - 如何使用 ppk 公钥通过 Python Paramiko 进行 ssh 连接

标签 python ssh putty paramiko public-key

我正在使用 Paramiko通过 SSH 连接到服务器。

基本身份验证运行良好,但我无法理解如何使用公钥连接。

当我连接 PuTTY 时,服务器会告诉我:

Using username "root".
Authenticating with public key "rsa-key@ddddd.com"
Passphrase for key "rsa-key@ddddd.com": [i've inserted the passphrase here]
Last login: Mon Dec  5 09:25:18 2011 from ...

我用这个 ppk 文件连接到它:

PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: rsa-key@dddd.com
Public-Lines: 4
[4 lines key]
Private-Lines: 8
[8 lines key]
Private-MAC: [hash]

使用基本身份验证,我得到的错误(来自日志)是:

DEB [20111205-09:48:44.328] thr=1   paramiko.transport: userauth is OK
DEB [20111205-09:48:44.927] thr=1   paramiko.transport: Authentication type (password) not permitted.
DEB [20111205-09:48:44.927] thr=1   paramiko.transport: Allowed methods: ['publickey', 'gssapi-with-mic']

我尝试包含该 ppk 文件并设置为 auth_public_key,但没有成功。

你能帮帮我吗?

最佳答案

好的,@Adam 和@Kimvais 是对的,Paramiko 无法解析 .ppk 文件。

所以要走的路(也感谢@JimB)是将 .ppk 文件转换为 OpenSSH 私钥格式;这可以使用 PuTTYgen 来实现如所述here .

那么连接起来就很简单了:

import paramiko
ssh = paramiko.SSHClient()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('<hostname>', username='<username>', password='<password>', key_filename='<path/to/openssh-private-key-file>')

stdin, stdout, stderr = ssh.exec_command('ls')
print stdout.readlines()
ssh.close()

关于python - 如何使用 ppk 公钥通过 Python Paramiko 进行 ssh 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8382847/

相关文章:

python - integer 在 Python 中被提升为 long 但反之则不然。为什么?

bash - Openssh 私钥到 RSA 私钥

linux - EC2 实例, "General error mounting filesystems"

ssh - 通过ssh连接时自动将cd传送到给定的远程路径

c - putty 如何检测 xfer 是否正在备份?

c - Putty 有时喜欢在我的命令行中打印自己

python - python opencv 如何检测 ROI 和周围的数字

python - 无事发生时显示 fps 为 0

ssh - 使用 PuTTY 自动执行命令/脚本

python - 从列表创建 Pandas 数据框