我是 Python 新手,我正在尝试制作一个连接到远程 Windows 机器并在那里执行命令并测试端口连接性的脚本。
这是我正在编写的代码,但它不起作用。基本上,我想要它返回本地机器数据,而不是远程数据。
import wmi
import os
import subprocess
import re
import socket, sys
def main():
host="remotemachine"
username="adminaam"
password="passpass!"
server =connects(host, username, password)
s = socket.socket()
s.settimeout(5)
print server.run_remote('hostname')
class connects:
def __init__(self, host, username, password, s = socket.socket()):
self.host=host
self.username=username
self.password=password
self.s=s
try:
self.connection= wmi.WMI(self.host, user=self.username, password=self.password)
self.s.connect(('10.10.10.3', 25))
print "Connection established"
except:
print "Could not connect to machine"
def run_remote(self, cmd, async=False, minimized=True):
call=subprocess.check_output(cmd, shell=True,stderr=subprocess.STDOUT )
print call
main()