简体   繁体   English

我怎么知道用户是否通过python脚本中的ssh连接到本地计算机?

[英]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.

相关问题 通过我的浏览器连接到ssh会话。 如何通过浏览器访问本地文件? - Connected to ssh session via my browser. How can I access my local files through the browser? 如何将某些数据从Google App脚本发布到本地计算机上的python脚本中? - How can I post some data from google app script to my python script on the local machine? 如何以其他用户(本地用户)的身份通过root运行python脚本,然后再返回root - How can I run a python script run via root as a different user (local user) then back to root ssh到远程机器,脚本通过python密码 - ssh to remote machine with password in script via python 我怎么知道在 HKEY_LOCAL_MACHINE 中创建注册表项的用户名 - how can i know the user name that created the registry key in HKEY_LOCAL_MACHINE 我如何获得 Python 以了解用户连接的 Wifi? - How do I get Python to know what Wifi the user is connected to? 我如何知道我的 Flask WebApp 的用户使用的是 Windows 还是 Linux 机器? - How can I know if a user of my Flask WebApp use a Windows or Linux machine? 如何通过SSH在python / bash / ruby​​脚本中使用“ sudo”执行命令? - How can I execute a command with “sudo” in a python/bash/ruby script via SSH? 如何通过Python脚本侦听和报告服务器(SSH)连接? - How can I listen for and report server (SSH) connections via a Python script? 如何使用 ssh 在远程机器上运行 python 脚本 - How run python script in remote machine with ssh
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM