[英]How can I know if the user is connected to the local machine via ssh in my python script?
我怎么知道用户是否通过python脚本中的ssh连接到本地计算机?
You can use the os
module to check for the existence of the environment variable SSH_CONNECTION
. 您可以使用
os
模块来检查环境变量SSH_CONNECTION
的存在。
>>> import os
>>> using_ssh = 'SSH_CONNECTION' in os.environ
Am I correct in assuming you're running your script on some sort of UNIX/Linux system? 我假设您在某种UNIX / Linux系统上运行脚本时是否正确? If so, you can just type "users" on the command-line, and it will show you the currently logged in users.
如果是这样,您只需在命令行上键入“用户”,它将显示当前登录的用户。
Also, if you call the "lastlog" command, that will show you all the users on the system and the last time they all logged in to the machine. 同样,如果您调用“ lastlog”命令,则将向您显示系统上的所有用户以及他们最后一次登录到计算机的时间。
Check any of the SSH variables SSH_CONNECTION, SSH_CLIENT, or SSH_TTY. 检查任何SSH变量SSH_CONNECTION,SSH_CLIENT或SSH_TTY。 However, these can be unset by the user.
但是,这些可以由用户取消设置。
Check the output of who am i. 检查我是谁的输出。 It will end with the remote system identification in brackets if you are connected remotely.
如果您是远程连接的,它将以括号中的远程系统标识结尾。 Make sure to handle x-term sessions which will have a colon (:) in the remote system id.
确保处理x-term会话,该会话在远程系统ID中将带有冒号(:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.