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. **
5 Answers
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).
5 Comments
pywinrm module link points to the exact same place as WSMAN library from Dell - I think that's a mistake. I found pywinrm here: pypi.python.org/pypi/pywinrm/0.0.3. It looks pretty good... if it allows me to upload and download files, it's perfect.pywinrm doesn't seem to have any way of doing uploads and downloads, but WinRM does. So maybe I can add in the capability to pywinrm myself...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.
4 Comments
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.
7 Comments
mobasshd and openSSH from Microsoft. For both these servers, I am unable to execute python scripts, even if I log in via ssh manually. I posted this in serverfault: serverfault.com/questions/901041/…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)
Comments
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.