How to ssh connect through Python Paramiko with ppk public key

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

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

Leave a Comment