我正在尝试在远程 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/