This Python program displays all users currently connected to the system via terminal or shell sessions.
It uses the who command executed through the subprocess module.
This is useful for monitoring active sessions or auditing user access.
import subprocess
# Run the 'who' command to list terminal users
result = subprocess.run(["who"], capture_output=True, text=True)
# Display output
print("\n--- Active Terminal Users ---")
print(result.stdout)
--- Active Terminal Users ---
shahin tty1 2025-10-01 08:42
admin pts/0 2025-10-01 09:15
- The who command lists users currently logged in via terminal
- Output includes username, terminal type (e.g. tty, pts), and login time
- subprocess.run executes the command and captures its output
- The result is printed directly to the console