远程 MS Windows 主机上的 Python 子进程

标签 python windows subprocess remote-access

我正在尝试在远程 Windows 主机(具有管理员权限的 Windows 域环境)上运行 netsh 命令。以下代码在本地主机上运行良好,但我也想使用 python 在远程主机上运行它。

import subprocess

netshcmd=subprocess.Popen('netsh advfirewall show rule name=\”all\”', shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE )
output, errors =  netshcmd.communicate()

问题是我不确定如何/使用什么方法来启动与远程主机的连接,然后运行子进程命令。我无法使用 ssh 或 pstools,如果可能的话,我想尝试使用现有的 pywin32 模块来实现它。

我过去使用过 WMI 模块,这使得查询远程主机变得非常容易,但我找不到任何方法通过 WMI 查询防火墙策略,这就是使用子进程的原因。

最佳答案

首先使用 pxssh 模块登录远程主机 Python: How can remote from my local pc to remoteA to remoteb to remote c using Paramiko

Windows远程登录:

child = pexpect.spawn('ssh tiger@172.16.0.190 -p 8888')
child.logfile = open("/tmp/mylog", "w")
print child.before
child.expect('.*Are you sure you want to continue connecting (yes/no)?')
child.sendline("yes")

child.expect(".*assword:")
child.sendline("tiger\r")
child.expect('Press any key to continue...')
child.send('\r')
child.expect('C:\Users\.*>')
child.sendline('dir')
child.prompt('C:\Users\.*>')

Python - Pxssh - Getting an password refused error when trying to login to a remote server

并发送您的 netsh 命令

关于远程 MS Windows 主机上的 Python 子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16013574/

相关文章:

windows - 导航到分组 GridView 中的特定组标题(win store app)

windows - 作为外部 .exe 启动与作为 Windows 服务启动,从性能角度来看,哪一个更有效?

python - Linux 上的子进程问题

python - 如何在python中创建具有负边权重的随机单源随机无环有向图

c++ - 使用 PBM_SETPOS 设置位置时 ProgressBar 滞后

python - 在 Python subprocess.Popen 函数中使用 ls

Python子进程: How to run a python script using a different intepreter than the main thread

python - 如何使用 Tkinter 中的类创建新页面?

python - 如何解决这个错误???引发HTTPError(req.full_url,代码,msg,hdrs,fp)HTTPError : Forbidden

python - 如何更新已弃用的 python zipline.transforms 模块?