Part of the series

Several example codes

~1 min read • Updated Oct 1, 2025

Program Overview

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.


Python Code:


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)

Sample Output:


--- Active Terminal Users ---
shahin   tty1         2025-10-01 08:42
admin    pts/0        2025-10-01 09:15

Step-by-Step Explanation:

- 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


Written & researched by Dr. Shahin Siami