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

2024/11/18 15:34:36

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.

Answer

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, StringIOkey_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)
https://en.xdnf.cn/q/71046.html

Related Q&A

SHA 512 crypt output written with Python code is different from mkpasswd

Running mkpasswd -m sha-512 -S salt1234 password results in the following:$6$salt1234$Zr07alHmuONZlfKILiGKKULQZaBG6Qmf5smHCNH35KnciTapZ7dItwaCv5SKZ1xH9ydG59SCgkdtsTqVWGhk81I have this snippet of Python…

Running python scripts in Anaconda environment through Windows cmd

I have the following goal: I have a python script, which should be running in my custom Anaconda environment. And this process needs to be automatizated. The first thing Ive tried was to create an .exe…

How to work out ComplexWarning: Casting complex values to real discards the imaginary part?

I would like to use a matrix with complex entries to construct a new matrix, but it gives me the warning "ComplexWarning: Casting complex values to real discards the imaginary part".As a resu…

Is it possible to use POD(plain old documentation) with Python?

I was wondering if it is possible to use POD(plain old documentation) with Python? And how should I do it?

ctypes error AttributeError symbol not found, OS X 10.7.5

I have a simple test function on C++:#include <stdio.h> #include <string.h> #include <stdlib.h> #include <locale.h> #include <wchar.h>char fun() {printf( "%i", 1…

Filtering negative timedeltas

Consider a series holding timedelta64[ns] that measures at the time difference between two events A and B:> time_deltas499900 -1 days +23:45:13 499916 -1 days +23:50:57 499917 00:03:2…

What is the Matlab equivalent of the yield keyword in Python?

I need to generate multiple results but one at a time, as opposed to everything at once in an array.How do I do that in Matlab with a generator like syntax as in Python?

Convert from CMYK to RGB

Im having trouble converting a single page pdf (CMYK) to a jpg (RGB). When I use the code below, the colors in the jpg image are garish. Ive tried reading through the Wand docs, but havent found anythi…

TopologicalError: The operation GEOSIntersection_r could not be performed

Hi Guys, I am trying to map the district shapefile into assembly constituencies. I have shape files for [Both].Basically I have to map all the variables given at district level in census data to assemb…

Plot a function during debugging in Python

I used to work in Matlab and it is really convenient (when working with big arrays/matrices and nested functions) to visualize intermediate results during debugging using plot function. In Python I can…