2

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 5

3

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).

Sign up to request clarification or add additional context in comments.

5 Comments

This sounds like the best solution. Can I use these modules to control Windows servers from Non-Windows, or do they rely on an underlying mechanism that would only be available on Windows?
the WMI module by Tim is only a wrapper on top of pywin32, so no, you couldn't use it on non-windows. I don't know about the Dell one (which looks abandoned now, btw). WRM in itself is an XML-based protocol (I think) so theoretically you could do a lot of things, but in practice it's one of those "enterprise" standards nobody really wants to get dirty with. If you really want, you can try looking at traffic and reimplement only the features you need.
The WMI is a no-go, being Windows only. The WSMAN documentation mentions that it supports Linux... maybe I can get it to work on OS X, too. It supports 2.6 and newer and was last updated 2 years ago, so it wasn't abandoned all that long ago. Your 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.
Hm. 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...
pywinrm may not have a way of doing downloads and uploads, but you can use pywinrm in combonation with pysmb to manage exactly that. However, a little more effort on the side of the remote host is needed since you have to enable file sharing.
2

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

It is my understanding that Windows 10 (to be released later this month), will have full support for SSH out of the box. So perhaps you won't need to install an SSH server on it if you can afford to only have it work with Windows 10 and newer. (I don't actually have any machines running the Windows 10 beta, so I haven't been able to test this yet.)
OpenSSH available from Microsoft is quite buggy. Its best to avoid. @ArtOfWarfare From their github page.. it seems a lot of people are having similar problems. Not sure whether it will work, but the best chance is to use one of the suggested sshd servers from here: serverfault.com/questions/8411/…
@ArtOfWarfare, I also have to execute python scripts on a remote windows machine. How did you resolve this issue?
@alpha_989 - I ended up installing sshd on it with cygwin. Sorry if that's not what you wanted to hear. The Windows 10 thing I mentioned previously... I played with that but most of our servers are still running Server 2008 and Server 2016 wasn't out yet at the time anyways.
2

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

I am wondering what sshserver you are running on Windows? I actually tried to do the same thing as you did with 2 different SSH servers: 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/…
Wondering if you could provide specific details of what libraries you are using?
In my case, I installed Python and then used the normal client/server socket protocol (docs.python.org/3/library/socket.html). As for running SSH on Windows, yeah, it's a world full of hurt. I don't have a working solution for that, either.
Thank you. It was kindof hard in the beginning.. but I discovered WinSSHD serverfault.com/a/8423/443721. Which turned out to be pretty stable and almost comparable to SSH on Unix systems and almost no bugs that I could find.
@alpha_989 Additional libraries mean I have to install them. In my case, I was checking for security patches which meant I had to read some data from the registry (using the win32 module), so I wanted to keep my footprint small.
|
1

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

0

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.

1 Comment

thanx for quick reply but as far as I know pyro4 is all abt calling python objects remotely but my scenario is that python is running on one system and other system is not having python installed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.