简体   繁体   中英

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 .

>>> 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? 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.

Check any of the SSH variables SSH_CONNECTION, SSH_CLIENT, or 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM