[python] How to ssh connect through python Paramiko with ppk public key

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.

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

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

I connect to it with this ppk file:

PuTTY-User-Key-File-2: ssh-rsa
Encryption: aes256-cbc
Comment: [email protected]
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']

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

Can you help me?

This question is related to python ssh putty paramiko public-key

The answer is


To create a valid DSA format private key supported by Paramiko in Puttygen.

Click on Conversions then Export OpenSSH Key

enter image description here


@VonC's answer to a duplicate question:

If, as commented, Paraminko does not support PPK key, the official solution, as seen here, would be to use PuTTYgen.

But you can also use the Python library CkSshKey to make that same conversion directly in your program.

See "Convert PuTTY Private Key (ppk) to OpenSSH (pem)"

import sys
import chilkat

key = chilkat.CkSshKey()

#  Load an unencrypted or encrypted PuTTY private key.

#  If  your PuTTY private key is encrypted, set the Password
#  property before calling FromPuttyPrivateKey.
#  If your PuTTY private key is not encrypted, it makes no diffference
#  if Password is set or not set.
key.put_Password("secret")

#  First load the .ppk file into a string:

keyStr = key.loadText("putty_private_key.ppk")

#  Import into the SSH key object:
success = key.FromPuttyPrivateKey(keyStr)
if (success != True):
    print(key.lastErrorText())
    sys.exit()

#  Convert to an encrypted or unencrypted OpenSSH key.

#  First demonstrate converting to an unencrypted OpenSSH key

bEncrypt = False
unencryptedKeyStr = key.toOpenSshPrivateKey(bEncrypt)
success = key.SaveText(unencryptedKeyStr,"unencrypted_openssh.pem")
if (success != True):
    print(key.lastErrorText())
    sys.exit()

For me I doing this:

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

works for me pretty ok


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to ssh

Starting ssh-agent on Windows 10 fails: "unable to start ssh-agent service, error :1058" How to solve "sign_and_send_pubkey: signing failed: agent refused operation"? key_load_public: invalid format ssh connection refused on Raspberry Pi Getting permission denied (public key) on gitlab Verify host key with pysftp Can't connect to Postgresql on port 5432 Checkout Jenkins Pipeline Git SCM with credentials? How to open remote files in sublime text 3 how to setup ssh keys for jenkins to publish via ssh

Examples related to putty

Forward X11 failed: Network error: Connection refused Best way to script remote SSH commands in Batch (Windows) AWS - Disconnected : No supported authentication methods available (server sent :publickey) Change mysql user password using command line How to download a file from my server using SSH (using PuTTY on Windows) Using putty to scp from windows to Linux Saving the PuTTY session logging Putty: Getting Server refused our key Error Google server putty connect 'Disconnected: No supported authentication methods available (server sent: publickey) Batch file for PuTTY/PSFTP file transfer automation

Examples related to paramiko

python paramiko ssh How to ssh connect through python Paramiko with ppk public key Paramiko's SSHClient with SFTP Running interactive commands in Paramiko How to scp in Python?

Examples related to public-key

key_load_public: invalid format Verify host key with pysftp Github permission denied: ssh add agent has no identities git push: permission denied (public key) Heroku 'Permission denied (publickey) fatal: Could not read from remote repository' woes Using scp to copy a file to Amazon EC2 instance? How to ssh connect through python Paramiko with ppk public key Adding a public key to ~/.ssh/authorized_keys does not log me in automatically Permission denied (publickey) when deploying heroku code. fatal: The remote end hung up unexpectedly GitHub: Permission denied (publickey). fatal: The remote end hung up unexpectedly