Python:os.environ.get('SSH_ORIGINAL_COMMAND')返回None

时间:2022-07-14 22:43:54

Trying to follow a technique found at bzr and gitosis I did the following:

试图遵循bzr和gitosis发现的技术我做了以下事情:

added to ~/.ssh/authorized_keys the command="my_parser" parameter which point to a python script file named 'my_parser' and located in /usr/local/bin (file was chmoded as 777)

添加到〜/ .ssh / authorized_keys命令=“my_parser”参数,该参数指向名为“my_parser”且位于/ usr / local / bin的python脚本文件(文件被编码为777)

in that script file '/usr/local/bin/my_parser' I got the following lines:

在该脚本文件'/ usr / local / bin / my_parser'中,我得到以下行:

#!/usr/bin/env python
import os
print os.environ.get('SSH_ORIGINAL_COMMAND', None)

When trying to ssh e.g. ssh localhost I get None on the terminal and then the connection is closed.

当试图ssh例如ssh localhost我在终端上获得None,然后连接关闭。

I wonder if anyone have done such or alike in the past and can help me with this.

我想知道过去是否有人这样或类似,可以帮助我。

Is there anything I should do in my python file in order to get that environment variable?

为了得到那个环境变量,我在python文件中应该做些什么?

1 个解决方案

#1


1  

$SSH_ORIGINAL_COMMAND is set when you connect to a host with ssh to execute a single command:

当您使用ssh连接到主机以执行单个命令时,将设置$ SSH_ORIGINAL_COMMAND:

$ ssh username@host 'some command'

Your "my_parser" would then return "some command".

然后你的“my_parser”会返回“some command”。

Unless you invoke a shell with my_parser, it will then exit, and the connection will close. You can use this to control the environment of the remotely executed commands, but you lose the ability to have an interactive session

除非使用my_parser调用shell,否则它将退出,连接将关闭。您可以使用它来控制远程执行命令的环境,但是您无法进行交互式会话

#1


1  

$SSH_ORIGINAL_COMMAND is set when you connect to a host with ssh to execute a single command:

当您使用ssh连接到主机以执行单个命令时,将设置$ SSH_ORIGINAL_COMMAND:

$ ssh username@host 'some command'

Your "my_parser" would then return "some command".

然后你的“my_parser”会返回“some command”。

Unless you invoke a shell with my_parser, it will then exit, and the connection will close. You can use this to control the environment of the remotely executed commands, but you lose the ability to have an interactive session

除非使用my_parser调用shell,否则它将退出,连接将关闭。您可以使用它来控制远程执行命令的环境,但是您无法进行交互式会话