如何通过公钥与python Paramiko连接

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

基本身份validation运作良好,但我不明白如何连接公钥。

当我连接腻子,服务器告诉我这一点:

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] 

使用基本身份validation我得到的错误(从日志)是:

 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不能parsing.ppk文件。

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

然后它很简单的获得连接:

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

– 看,对我来说,我这样做:

 import paramiko hostname = 'my hostname or IP' myuser = 'the user to ssh connect' mySSHK = '/path/to/sshkey.pub' sshcon = paramiko.SSHClient() # will create the object sshcon.set_missing_host_key_policy(paramiko.AutoAddPolicy())# no known_hosts error sshcon.connect(hostname, username=myuser, key_filename=mySSHK) # no passwd needed 

– 为我工作很好

在Puttygen中创build由Paramiko支持的有效DSA格式私钥。

点击转换,然后导出OpenSSH密钥

在这里输入图像说明