gpt4 book ai didi

python - 将 secret 传递给通过 SSH 启动的程序

转载 作者:太空宇宙 更新时间:2023-11-04 05:09:06 24 4
gpt4 key购买 nike

我正在通过 SSH 启动一个 Python 程序,我想向它传递一个 secret 。

ssh remote python program.py

我控制程序的代码,这样我就可以实现我想要的任何方法。我考虑过以下选项:

使用命令行参数

ssh remote python program.py --secret=abc

这不起作用,因为本地和远程计算机上的任何用户都可以看到使用此参数调用了 SSH 和程序。

使用 TCP

ssh -L 1234:localhost:1234 remote python progam.py

该程序将监听端口 1234 并等待我通过连接发送 secret 。这也不起作用,因为任何程序都可以连接到端口 1234 并将垃圾 secret 传递给 program.py

使用标准输入

cat secret.txt | ssh remote python program.py

这可行,但不幸的是,对于我的用例,stdin 已用于将其他数据传递给程序。

我还有其他选择吗? stdin 是唯一的方法吗?

最佳答案

Is stdin the only way?

前提是您不能使用套接字(TCP 或 UDP)将 secret 信息传输到远程;考虑到您提到的约束以及描述问题的方式,似乎 stdin 是唯一的方法。

套接字将为您提供一个文件描述符接口(interface),您可以在该接口(interface)上将本地文件写入远程端。由于您无法使用它,stdin 仍然是基于消息的应用程序协议(protocol)的实用选项。见下文。

Do I have any other options?

是的,您还有很多其他选择。即,您可以通过创建对象并从 program.py 内部执行 read/write 来创建基于消息的协议(protocol)。

class Message:
SECRET, INFO = range(2) # see also: from enum import auto, Enum
#https://docs.python.org/3.7/library/enum.html

def __init__(self, type_, content_):
self.type = type_
self.content = content_


msec = Message(Message.SECRET, "secret here")
minf = Message(Message.INFO, "clear info here")

关于python - 将 secret 传递给通过 SSH 启动的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57844111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com