0

I am generating private key from paramiko and tried connecting to my another machine using below code. I have saved key generated in .pub file. I have copied the same key in '~/.ssh/authorised_keys' file in my linux machine. I get error AuthenticationException: Authentication failed when I tried to connect.

import paramiko

key = paramiko.RSAKey.generate(1024)
fp = open('path\rsa_python.pub', 'w')
key.write_private_key(fp)
fp.close()
## Copied key to linux machine.

k = paramiko.RSAKey.from_private_key_file(r'path\rsa_python.pub')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('linux_machine', username='***', pkey=k)

I am not getting where I am going wrong. Please help me with some code.

Deepak
  • 119
  • 8
  • I'm not an expert with rsa but, shouldn't the private key go in `/home/Deepak/.ssh/id_rsa` and the public key in `/home/Deepak/.ssh/id_rsa.pub`. then you store your `id_rsa.pub` on the remote server in `.ssh/authorized_keys`. Afterward you show you private key to the remote server, it validate it with the `id_rsa.pub` you gave him – pwnsauce Jul 21 '16 at 13:13
  • @pwnsauce , paramiko.RSAKey.generate(1024) gives me only 1 key file. I assume it's private key. Don't know how to go with it. I have tried using the keys generated from puttygen.exe . But the paramiko throws error(wrong file format) while reading the private.ppk file. – Deepak Jul 22 '16 at 03:28
  • "From within puttygen I think you may 'export' the key in order to get a suitable format for OpenSSH which should be compatible with paramiko." [source](http://blog.gmane.org/gmane.comp.python.paramiko/month=20090201) You have to export the putty keys as openssh [here is how to](http://stackoverflow.com/questions/2224066/how-to-convert-ssh-keypairs-generated-using-puttygenwindows-into-key-pairs-use) – pwnsauce Jul 22 '16 at 07:41

0 Answers0