Run commands on a remote Windows host using SMB/RPC

0.3.0 · maintenance · verified Thu Apr 16

pypsexec is a Python library that enables running commands on a remote Windows host via SMB/RPC, similar to the popular PsExec tool. It achieves this by deploying and using a bundled PAExec executable on the remote target. The library is currently at version 0.3.0, with the last major release in October 2021, suggesting a maintenance release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This example connects to a remote Windows host, creates a temporary service to execute a command ('whoami.exe /all'), captures its output, and then cleans up the service and connection. Credentials are pulled from environment variables.

import os
from pypsexec.client import Client

hostname = os.environ.get('PSEXEC_HOST', 'your_windows_host')
username = os.environ.get('PSEXEC_USERNAME', 'your_username')
password = os.environ.get('PSEXEC_PASSWORD', 'your_password')

if not all([hostname, username, password]):
    print("Please set PSEXEC_HOST, PSEXEC_USERNAME, PSEXEC_PASSWORD environment variables.")
    exit(1)

c = Client(hostname, username=username, password=password, encrypt=True)
try:
    c.connect()
    c.create_service()
    # Run 'whoami.exe /all' command
    stdout, stderr, rc = c.run_executable('whoami.exe', arguments='/all')

    print(f"STDOUT:\n{stdout.decode('utf-8') if stdout else ''}")
    print(f"STDERR:\n{stderr.decode('utf-8') if stderr else ''}")
    print(f"Return Code: {rc}")
finally:
    c.remove_service()
    c.disconnect()

view raw JSON →