Python:使用wmi远程启动可执行文件

时间:2015-04-15 19:46:09

标签: python windows wmi executable

我试图使用wmi模块在Windows上远程运行可执行文件。它建立了连接,但我认为我的流程线是不正确的,因为当我检查服务器时,肯定还没有运行可执行文件。你能帮我解决一下语法吗?

import wmi, time
ip = '10.12.62.186'
username = "administrator"
password = "CLARiiON!"
from socket import *
print "Establishing connection to %s" %ip
connection = wmi.WMI(ip, user=username, password=password)
print "Connection established"
print "Starting IO"
connection.Win32_Process.Create(CommandLine='cmd.exe C:\Users\Public\Desktop\Auto_IOX.exe')
time.sleep(60)

2 个答案:

答案 0 :(得分:4)

看看Tim Golden的tutorial。您没有使用Win32_Process.Create方法返回的信息。

process_id, result = c.Win32_Process.Create(
  CommandLine="notepad.exe",
  ProcessStartupInformation=process_startup
)

因此,您可能错过了进程ID以及启动该远程进程的结果。

答案 1 :(得分:4)

import wmi, time
ip = 'xx.xx.xx.xxx'
username = "user"
password = "password!"
SW_SHOWNORMAL = 1
from socket import *
print "Establishing connection to %s" %ip
c = wmi.WMI(ip, user=username, password=password)
process_startup = c.Win32_ProcessStartup.new()
process_startup.ShowWindow = SW_SHOWNORMAL
process_id, result = c.Win32_Process.Create(CommandLine="C:\User\Administrator\Desktop\runIOX_auto.bat",ProcessStartupInformation=process_startup)
if result == 0:
  print "Process started successfully: %d" % process_id
else:
  raise RuntimeError, "Problem creating process: %d" % result

我设法找出它(在D'Day的帮助下)创建一个批处理文件,该文件运行我需要的所有内容并将其放在桌面上然后再运行它。