this time i am asking about running commands on remote Windows machine. Let me be more descriptive here.
**I have a machine on which python is installed and I want to run some powershell and cmd commands or I want to send a cmd file to remote windows machine so that it can run in there and every output like error, stdout I can get back to the firing Machine.
Like I want to manage remote machines remotely from python. And if possible can i fire commands remotely on multiple machines simultaniously with python.
Remember that other system does n ot have any python installed on. **
It's a bit of a minefield, but the "right" way to do this (python controlling a remote win machine without python) is using a library for WMI or WRM.
There's a WMI module by Tim Golden (who's an authority in the Python-on-Windows world), the WSMAN library from Dell , and a pywinrm module . Pick your poison -- they all have their own weak spots (WRM has to be enabled on server before it can be used, although this doesn't require any extra software, it's all stock Windows; WMI works over DCOM, so you'll have to figure out DCOM security, etc etc).
You can use ssh
for this as khagler suggested. But I prefer to install Python on both sides because Python gives me much more control on the remote side.
For example, ssh
just offers a single channel between the two computers. That means I have to fold stdout
and stderr
on the remote side to send it over the wire. With a remote Python server, I can run more complex scripts, I can control exactly what I want to expose (if you allow remote access any command can be executed) and the error handling is much more simple.
Or a combination of the two: Install Python on the remote computer and then use ssh
to start a Python script. You can then communicate with it with a custom protocol, without the limits of the command line/shell interface. You can even use scp
to install new scripts remotely.
Knowing this is a quite old post,
The best option I can think of as of now is to use PowerShell for Window Remote Management.
Below is how to execute a command in remote PC using PowerShell and how to execute the same in Python.
PowerShell to start a service:
Get-Service -ComputerName aaa0001 -Name Tomcat9 | Start-Service
Now, next is how to execute this with Python,
import subprocess
subprocess.call(["C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe","Get-Service -ComputerName aaa0001 -Name Tomcat9 | Start-Service"])
This example shows how to start Tomcat9 windows service in Windows. ( "aaa0001" is just the computer name)
You could try using something like Pyro4 in combination with subprocess.popen
.
Pyro4 is a framework for remote method invocation, and subprocess.popen
is used for creating underlying OS processes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.