【发布时间】:2017-08-22 19:22:29
【问题描述】:
我正在尝试连接到远程 Windows 机器并从命令行执行一些命令。命令就像:我在某个文件夹中有可执行文件,转到该文件夹并运行命令
InstallUtil.exe <exe_name>
这是我的代码:
class WindowsMachine:
def __init__(self, hostname, username, password):
self.hostname = hostname
self.username = username
self.password = password
# self.remote_path = hostname
try:
print("Establishing connection to .....%s" %self.hostname)
connection = wmi.WMI(self.hostname, user=self.username, password=self.password)
print("Connection established")
try:
print(os.listdir(r"C:\Program Files\BISYS\BCE"))
a = subprocess.check_output(["InstallUtil.exe","IamHere.exe"], cwd="C:/Program Files/ABC/BCD/",stderr=subprocess.STDOUT)
print(a)
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
except wmi.x_wmi:
print("Could not connect to machine")
raise
w = WindowsMachine(hostname,username,password)
print(w)
print(w.run_remote())
但我收到错误消息:
WindowsError: [Error 2] The system cannot find the file specified
【问题讨论】:
-
出现此错误消息是因为未找到
InstallUtil.exe。你确定你可以从命令行运行它吗? -
是的。我去了那个目录并执行了命令:
InstallUtil.exe <exename>.exe,它工作了..我也试过installutll <exename>.exe -
您可能需要在命令执行时更改当前目录。也许你应该试试
subprocess.check_output(["InstallUtil.exe"," IamHere.exe"],cwd="C:/Program Files/ABC/BCD",stderr=subprocess.STDOUT) -
如果我在命令行
installutill C:/Program Files/ABC/DEF/Iamhere.exe上发出命令,它可以工作,但不是来自我的脚本 -
错误是:
WindowsError: [Error 267] The directory name is invalid
标签: python subprocess wmi pywin32