在 Paramiko 应用程序中将密钥作为字符串嵌入 [英] Embedding key as string in Paramiko application

查看:182
本文介绍了在 Paramiko 应用程序中将密钥作为字符串嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Python 中创建单个文件可执行文件,并在我的 SSH 中使用 Paramiko.我需要消除私钥文件等外部文件,并尝试使用嵌入的字符串.

I'm trying to create a single file executable in Python and using Paramiko for my SSH. I need to eliminate external files such as private key files and try to go for embedded strings.

我尝试了这个解决方案,但它对我不起作用:
Paramiko:从公钥字符串创建 PKey

I tried this solution but it's not working for me:
Paramiko: Creating a PKey from a public key string

我如何做到这一点?谢谢.

How do I accomplish this? Thanks.

推荐答案

你提到的解决方案:

key = paramiko.RSAKey(data=base64.b64decode('AAblablabla...'))

工作正常,但以 base64 格式存储密钥可能不方便.

works fine however it may be inconvenient to store the key in base64 format.

以下代码显示了如何使用存储在纯文本"中的密钥.格式(作为 ~/.ssh 目录中的密钥文件):

The following code shows how to use the key stored in "plain-text" format (as key-files in ~/.ssh directory):

import paramiko
import StringIO

my_key = """\
-----BEGIN RSA PRIVATE KEY-----
<your key here>
-----END RSA PRIVATE KEY-----"""

pkey = paramiko.RSAKey.from_private_key(StringIO.StringIO(my_key))

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='host', username='user', pkey=pkey)

...

ssh.close()

这篇关于在 Paramiko 应用程序中将密钥作为字符串嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆