Running commands on Remote Windows machine with python

不问归期 提交于 2019-12-12 16:30:38

问题


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


回答1:


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




回答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.




回答3:


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.




回答4:


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)




回答5:


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.



来源:https://stackoverflow.com/questions/13627485/running-commands-on-remote-windows-machine-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!