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. **
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can install an ssh server on the Windows machine (I've heard freeSSHd is good), and then use the paramiko module to communicate with it.
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 foldstdout
andstderr
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 usescp
to install new scripts remotely.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).
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:
Now, next is how to execute this with Python,
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.