Python使用wmi远程Windows机器执行命令

 

一、准备

安装wmi和pywin32

 

pip install wmi
pip install pywin32

一、代码

 

import wmi
def sys_version(ipaddress, user, password):
    try:
        conn = wmi.WMI(computer=ipaddress, user=user, password=password)
        for sys in conn.Win32_OperatingSystem():
            print ("Version:%s" % sys.Caption, "Vernum:%s" % sys.BuildNumber ) # 系统信息
            print (sys.OSArchitecture.encode("UTF8"))  # 系统的位数
            print (sys.NumberOfProcesses ) # 系统的进程数

            filename = r"C:\test.bat"  # 此文件在远程服务器上
            cmd_callbat = r"cmd /c call %s" % filename
            print(cmd_callbat)
            id, value =conn.Win32_Process.Create(CommandLine=cmd_callbat)  # 执行bat文件   Win32_Process.Create
            print(id,value)
    except Exception as e:
        print (e)
        print (e.com_error.strerror.encode("UTF8"))


if __name__ == '__main__':
    sys_version(ipaddress="192.168.68.132", user="Administrator", password="123")

 

posted @ 2020-02-21 20:37  HuTiger  阅读(4151)  评论(2编辑  收藏  举报