从字符串中提取paramiko中的私钥如何包含? [英] How to include the private key in paramiko after fetching from string?
问题描述
我正在和paramiko合作,我已经生成了我的私钥,并尝试这样做很好。现在我正在使用基于Django的应用程序,我已经在数据库中复制了私钥。
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.
我将私钥保存在 charField
在Django模型。我在以下代码中遇到问题:
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=?)
在这种情况下应该怎么办?当我将数据库中的 string
存储在 SSH.connect
中时,应该如何传递私钥?
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?
即使我不能使用IO,即从数据库获取密钥并将其写入文件,然后使用保存的文件对象并传入 from_private_key(cls,file_obj,password = None)
在此输入代码
,因为这是web并且它不是通用密钥。每个用户都有自己的私钥。
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 here
because this is web based application and it is not a generic key. Every user have it's own private key.
推荐答案
您可以使用 StringIO 用于存储私钥和 PKey 的类方法 from_private_key ,因此您将创建 PKey 实例,您可以将其传递给连接方法。
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.
这篇关于从字符串中提取paramiko中的私钥如何包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!