如何使用ppk公钥通过python Paramiko进行ssh连接 [英] How to ssh connect through python Paramiko with ppk public key

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

问题描述

我正在使用 Paramiko 通过 ssh 连接到服务器.

i'm using Paramiko to connect through ssh to a server.

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

Basic authentication works well, but i can't understand how to connect with public key.

当我用 putty 连接时,服务器告诉我:

When i connect with putty, the server tell me this:

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 文件连接到它:

I connect to it with this ppk file:

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]

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

With basic auth the error i get (from the log) is:

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,但没有奏效.

I've tried to include that ppk file and set to auth_public_key, but didn't work.

你能帮我吗?

推荐答案

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

Ok @Adam and @Kimvais were right, paramiko cannot parse .ppk files.

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

So the way to go (thanks to @JimB too) is to convert .ppk file to openssh private key format; this can be achieved using Puttygen as described here.

然后与它建立联系非常简单:

Then it's very simple getting connected with it:

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()

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

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