【问题标题】:Subprocess to execute commands on Windows machine在 Windows 机器上执行命令的子进程
【发布时间】: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


【解决方案1】:

如 cmets 中所述,如果您的目标是首先在远程 Windows 机器上运行进程,则应保持远程连接打开,因此不要将其存储在本地变量 connection 中,而是将其存储在类成员 @987654323 中@。 现在,要使用 WMI 在远程机器上执行命令,您应该执行类似this(WMI tutorial):

class ConnectToRemoteWindowsMachine:
      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)
              self.connection = wmi.WMI(self.hostname, user=self.username, password=self.password)
              print("Connection established")
          except wmi.x_wmi:
              print("Could not connect to machine")
              raise

      def run_remote(self, async=False, minimized=True):

          SW_SHOWNORMAL = 1

          process_startup = self.connection.Win32_ProcessStartup.new()
          process_startup.ShowWindow = SW_SHOWNORMAL

          process_id, result = c.Win32_Process.Create(
              CommandLine="notepad.exe",
              ProcessStartupInformation=process_startup
          )
          if result == 0:
              print "Process started successfully: %d" % process_id
          else:
              raise RuntimeError, "Problem creating process: %d" % result


w = ConnectToRemoteWindowsMachine(hostname,username,password)
print(w)
print(w.run_remote())

【讨论】:

  • 有效吗?它是打印 process_id 还是打印错误消息?
  • 是的,它打印进程ID和返回值为0
  • afaik 用 WMI 是不可能的
猜你喜欢
  • 2011-07-13
  • 2020-07-25
  • 2012-07-27
  • 2015-01-19
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
  • 2017-04-17
  • 1970-01-01
相关资源
最近更新 更多