Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include the private key in paramiko after fetching from string?

I am working with paramiko, I have generated my private key and tried it which was fine. Now I am working with Django based application where I have already copied the private key in database.

I saved my private key in charField in Django model. I am facing a problem in the following code:

host = "192.154.34.54"
username = "lovestone"
port = 25
pkey = "------" # I saved my key in this string
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server, username=username, port=port, pkey=?)

What should I do in this case? How should I pass the private key in SSH.connect while I stored it in string in database?

Updated

Even i can't use the IO i.e. fetch the key from database and write it to a file and then use the saved file object and pass into from_private_key(cls, file_obj, password=None) enter code herebecause this is web based application and it is not a generic key. Every user have it's own private key.

like image 250
Amit Pal Avatar asked Aug 16 '12 19:08

Amit Pal


People also ask

How do I SSH to a private key in Python?

Just add your public keys to the file ~/. ssh/authorized_keys -- it's a plain-text file with one key per line. If you don't have a public/private key pair yet, you can generate one by running the following command on your system: $ ssh-keygen -t rsa -b 2048 Generating public/private rsa key pair.

What is Paramiko SSHClient ()?

SSHClient. A high-level representation of a session with an SSH server. This class wraps Transport , Channel , and SFTPClient to take care of most aspects of authenticating and opening channels. A typical use case is: client = SSHClient() client.

How do I get stdout from Paramiko?

Using exec_command() in Paramiko returns a tuple (stdin, stdout, stderr) . Most of the time, you just want to read stdout and ignore stdin and stderr . You can get the output the command by using stdout. read() (returns a string) or stdout.


2 Answers

if you cannot save the private keys to files, that would be the the main reason to use StringIO: StringIO takes a string and turns it into a file-like object, but nothing is written to the file system. It allows you to pass in file-like objects when all you have is a string: i.e. exactly cases like this.

import paramiko, StringIO

key_string = "------" # I saved my key in this string
not_really_a_file = StringIO.StringIO(key_string)
private_key = paramiko.RSAKey.from_private_key(not_really_a_file)

not_really_a_file.close()

That works for me, and you should be able to connect with that object:

client = paramiko.SSHClient()
client.set_missing_key_policy(paramiko.AutoAddPolicy())
client.connect(host="example.com", username="root", pkey=private_key)
like image 124
gbromios Avatar answered Sep 24 '22 07:09

gbromios


You can use StringIO for creating file-like object with your stored private key and PKey's class method from_private_key, so you will create PKey instance and you can pass it to connect method.

like image 39
Fedor Gogolev Avatar answered Sep 20 '22 07:09

Fedor Gogolev