How to enable private key in paramiko after extracting from string?

I work with paramiko, I created my secret key and tried it, which was good. Now I am working with a Django application, where I have already copied the private key in the database.

I saved my private key in charFieldin the Django model. I ran into 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 to transfer the private key to SSH.connect, but I saved it stringin the database?

Update

Even I can not use IO, i.e. retrieve the key from the database and write it to a file, and then use the saved file object and go to from_private_key(cls, file_obj, password=None) enter code here, because it is a web application and it is not a shared key. Each user has their own key.

+5
2

, StringIO: StringIO - , . , , , , : , .

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

, :

client = paramiko.SSHClient()
client.set_missing_key_policy(paramiko.AutoAddPolicy())
client.connect(host="example.com", username="root", pkey=private_key)
+4

All Articles