使用python的'wmi'远程执行程序

时间:2018-08-03 06:36:54

标签: python python-3.x remote-access execute

我正在尝试使用wmi库在远程计算机上执行程序或应用程序。这是我通过谷歌搜索找到的代码:

ip = <ipaddress_of_remote>
username = <username_of_remote>
password = <password_of_remote>

try:
    SW_SHOWNORMAL = 1
    print ("Establishing connection to %s" %ip)
    connection = wmi.WMI(ip, user=username, password=password)
    print ("Connection established")
    process_startup = connection.Win32_ProcessStartup.new()
    process_startup.ShowWindow = SW_SHOWNORMAL
    process_id, result = connection.Win32_Process.Create(CommandLine=r"cmd", ProcessStartupInformation=process_startup)
    if result == 0:
        print ("Process started successfully: %d" % process_id)
    else:
        raise (RuntimeError, "Problem creating process: %d" % result)

except wmi.x_wmi:
    print ("Your Username and Password of "+getfqdn(ip)+" are wrong.")

此代码工作正常,它与远程计算机建立连接,并通过打印process_pid返回Process started successfully: pid。 但是它实际上并没有在远程计算机上执行任何操作。我在connection.Win32_Process.Create()中做错了吗?

我实际上想使用此代码执行python脚本。我正在尝试在此代码中启动命令提示符。请提供命令以在远程计算机上执行python脚本。

0 个答案:

没有答案