I know to access tasklist of our machine can be done through os.popen('tasklist').read().

Is there any way to read the tasklist (taskmanager data) of other systems connected in a network using Python?

Just reading them will be fine.

Purpose: Main objective of this is to write a script which will say who has connected to the remote desktop( 'mstsc.exe' in tasklist). Suggest if there is any other way to do this. Thanks

Dani AI

Generated

A few practical options depending on the actual goal.

If the aim is “who connected to this machine’s Remote Desktop” then looking for mstsc.exe on the server is unreliable: mstsc.exe is the RDP client and usually runs on the client machine. To detect incoming RDP sessions, enumerate logon/session information (Win32_LogonSession with LogonType=10), use query user/qwinsta, or inspect Security event logs for logon events. If instead you want to inventory processes across many hosts (e.g. who is running the RDP client on their workstation), you can remotely list processes with tasklist, WMI, PowerShell remoting or PsExec.

As hinted, tasklist does support remote hosts; use CSV output to make parsing easy. You don’t need to “read the screen” and clear it (that’s the approach suggested) — capture command output programmatically and parse it. Example (quick, works without extra Python packages):

import subprocess, csv

out = subprocess.check_output(
    ['tasklist', '/s', 'REMOTE_HOST', '/u', r'DOMAIN\user', '/p', 'password', '/fo', 'csv', '/nh'],
    universal_newlines=True
)
for row in csv.reader(out.splitlines()):
    proc_name, pid = row[0], row[1]
    if proc_name.lower() == 'mstsc.exe':
        print(proc_name, pid)

More robust: use WMI from Python (pywin32 / wmi module) to query Win32_Process or to enumerate Win32_LogonSession (LogonType 10) and map to accounts. Example process check with wmi:

import wmi
c = wmi.WMI(computer='REMOTE_HOST', user=r'DOMAIN\user', password='password')
for p in c.Win32_Process(Name='mstsc.exe'):
    print(p.ProcessId, p.Name)

Caveats: remote methods require appropriate credentials and remote-management enabled (WMI/WinRM/SMB), and firewalls/UAC/group policy can block access. Don’t store plaintext credentials; prefer integrated auth or secure vaults. For accurate “who connected to this box” use session enumeration or event logs rather than searching for mstsc.exe.

Recommended Answers

All 4 Replies

Dos' tasklist command seems to support connecting to a remote system.

could u pl explain more about this ??

Read the doc

It writes the output to the screen, so you'll have to somehow read the screen an then clear it (try doing this for clearing the screen:

import subprocess
import platform

def clear():
    subprocess.Popen( "cls" if platform.system() == "Windows" else "clear", shell=True)

clear()

)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.