- 1 year ago
Trying to remotely start a process with visible window with Python on Windows machines
To remotely start a process with a visible window on a Windows machine, you can use the subprocess
module in Python along with PsExec, a command-line tool that allows you to execute processes on remote machines. PsExec is part of the PsTools suite, which you can download from the Microsoft Sysinternals website.
Here's a step-by-step guide to achieve this:
Download PsTools:
Download the PsTools suite from the Microsoft Sysinternals website and extract it to a folder on your local machine. Make sure to have PsExec (psexec.exe
) in the same directory as your Python script.Install
pywinrm
:
Install thepywinrm
library, which allows you to remotely execute commands using the Windows Remote Management (WinRM) protocol. You can install it using pip:bashpip install pywinrm
Python Script:
Write a Python script to remotely start the process on the target machine. Replace the placeholders with your actual values.pythonimport subprocess
# PsExec path on your local machine
PSEXEC_PATH = r'path\to\PsExec\psexec.exe'
# Target machine hostname or IP address
REMOTE_HOST = 'target_machine'
# Credentials for the remote machine
REMOTE_USER = 'remote_username'
REMOTE_PASSWORD = 'remote_password'
# Command to be executed on the remote machine
COMMAND = r'c:\path\to\your\program.exe'
# Arguments for the command (if any)
ARGUMENTS = 'arg1 arg2 arg3'
# Build the PsExec command
psexec_cmd = [
PSEXEC_PATH,
fr'\\{REMOTE_HOST}',
'-u',
fr'{REMOTE_USER}',
'-p',
fr'{REMOTE_PASSWORD}',
'-i', # Interactive mode to show the window
'0', # Session ID 0 (console session)
'-d', # Don't wait for the process to finish
COMMAND,
ARGUMENTS,
]
# Run the PsExec command to start the process on the remote machine
subprocess.run(psexec_cmd)
Make sure to replace the placeholders (
path\to\PsExec\psexec.exe
,target_machine
,remote_username
,remote_password
,c:\path\to\your\program.exe
,arg1 arg2 arg3
) with your actual values.Run the Python Script:
Save the Python script and run it. It should start the specified process with a visible window on the target machine.
Please note that using PsExec to execute processes remotely requires administrative privileges on both the local and remote machines. Additionally, it's essential to ensure the security of your system when using remote execution.