Python-模块-wmi

import wmi

wwmi.WMI()

for processor in w.Win32_Processor():

print "Processor ID: %s" % processor.DeviceID

print "Process Name: %s" %processor.Name.strip()

totalMemSize0

for memModule in w.Win32_PhysicalMemory():

totalMemSize+int(memModule.Capacity)

print "Memory Capacity: %.2fMB"%((totalMemSize+1048575)/1048576

列出所有进程

 

import wmi

c wmi.WMI ()

for process in c.Win32_Process ():

print process.ProcessId, process.Name

列出所有记事本进程

 

import wmi

c wmi.WMI ()

for process in c.Win32_Process(name"notepad.exe"):

print process.ProcessId, process.Name

创建然后删除记事本进程

 

import wmi

c wmi.WMI ()

process_id, return_value c.Win32_Process.Create(CommandLine"notepad.exe")

for process in c.Win32_Process (ProcessIdprocess_id):

print process.ProcessId, process.Name

result process.Terminate ()

列出创建进程的调用方法

 

import wmi

c wmi.WMI ()

print c.Win32_Process.Create

显示为自动当前未启动的服务

 

import wmi

c wmi.WMI ()

stopped_services c.Win32_Service(StartMode"Auto", State"Stopped")

if stopped_services:

for s in stopped_services:

print s.Caption, "service is not running"

else:

print "No auto services stopped"

显示磁盘使用百分比

 

import wmi

c wmi.WMI ()

for disk in c.Win32_LogicalDisk (DriveType3):

print disk.Caption, "%0.2f%% free" % (100.0 *long (disk.FreeSpace) / long (disk.Size))

运行记事本,等待直到它关闭,并且显示内容

 

import wmi

c wmi.WMI ()

filename r"c:temptemp.txt"

process c.Win32_Process

process_id, result process.Create(CommandLine"notepad.exe " + filename)

watcher c.watch_for (

notification_type"Deletion",

wmi_class"Win32_Process",

delay_secs1,

ProcessIdprocess_id

)

watcher ()

print "This is what you wrote:"

print open (filename).read ()

我运行的时候出现错误,filename似乎是获取的一个临时文件名而非我保存的文件名因此不能read不能显示出内容。

监视一个打印任务

 

import wmi

c wmi.WMI ()

print_job_watcher c.watch_for (

notification_type"Creation",

wmi_class"Win32_PrintJob",

delay_secs1

)

#

# Or, from 1.0 rc3 onwards

#

# print_job_watcher c.Win32_PrintJob.watch_for (

# notification_type"Creation",

# delay_secs1

# )

while 1:

pj print_job_watcher ()

print "User %s has submitted %d pages to printer %s" %

(pj.Owner, pj.TotalPages, pj.Name)

重启远程计算机

 

import wmi

# other_machine "machine name of your choice"

c wmi.WMI (computerother_machine,privileges["RemoteShutdown"])

os c.Win32_OperatingSystem (Primary1)[0]

os.Reboot ()

我想应该有权限才对,反正我是没有关起。另外,名字可以用IP代替吗?

显示IP和MAC

 

import wmi

c wmi.WMI ()

for interface in c.Win32_NetworkAdapterConfiguration (IPEnabled1):

print interface.Description, interface.MACAddress

for ip_address in interface.IPAddress:

print ip_address

print

自启动程序和位置

 

import wmi

c wmi.WMI ()

for s in c.Win32_StartupCommand ():

print "[%s] %s <%s>" % (s.Location, s.Caption,s.Command)

 

简介: 

本文所有的例均是假设你在使用来自http://timgolden.me.uk/python/wmi/cookbook.html的WMI模块。使用此模块,你可以在Windows系统中去体验下面这些实用的例子。或许你将由此了解到WMI的冰山一角。 

下面这些例子,除非有特别说明,均假设你要连接的是当前的机器。如果要连接远程机器,只需要在WMI构造器中指定远程机器名即可:

?

1

2

import wmi

c = wmi.WMI("some_other_machine")

注:这都是些完整的例子,你可以直接复制粘贴到一个.py文件里面,也可以复制粘贴到Python命令行交互窗口(原文作者是在Windows2000系统的CMD窗口做的测试)。 

实例: 

列出所有正在运行的进程

 

?

1

2

3

4

5

import wmi

c = wmi.WMI()

 

for process in c.Win32_Process():

  print process.ProcessId, process.Name

列出所有正在运行的记事本进程 

 

?

1

2

3

4

5

import wmi

c = wmi.WMI()

 

for process in c.Win32_Process(name="notepad.exe"):

  print process.ProcessId, process.Name

创建一个新的记事本进程然后结束它

 

?

1

2

3

4

5

6

7

8

import wmi

c = wmi.WMI()

 

process_id, return_value = c.Win32_Process.Create(CommandLine="notepad.exe")

for process in c.Win32_Process (ProcessId=process_id):

  print process.ProcessId, process.Name

 

result = process.Terminate()

显示Win32_Process类的.Create方法的接口 

注:wmi模块会接受WMI方法的传入参数作为Python的关键字参数,并把传出参数作为一个元组进行返回。

 

?

1

2

3

4

import wmi

c = wmi.WMI()

 

print c.Win32_Process.Create

显示没有处于正常运行状态的自启动服务

 

?

1

2

3

4

5

6

7

8

9

import wmi

c = wmi.WMI()

 

stopped_services = c.Win32_Service(StartMode="Auto", State="Stopped")

if stopped_services:

  for s in stopped_services:

    print s.Caption, "service is not running"

else:

  print "No auto services stopped"

显示每个固定磁盘的剩余空间百分比

 

?

1

2

3

4

5

import wmi

c = wmi.WMI()

 

for disk in c.Win32_LogicalDisk(DriveType=3):

  print disk.Caption, "%0.2f%% free" %(100.0 * long(disk.FreeSpace) / long(disk.Size))

运行记事本,等它关闭之后显示它里面的文字 

注:这个例子是运行一个进程并且知道它什么时候结束,而不是去处理输入到记事本里面的文字。所以我们只是简单的用记事本打开一个指定文件,等到用户完成输入并关闭记事本之后,显示一下它的内容。 

本例不适用于远程机器,因为处于安全考虑,在远程机器上启动的进程是没有界面的(你在桌面上是看不到它们的)。这类远程操作的技术多用于在服务器上运行一个安装程序,安装结束之后重启机器。

 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

import wmi

c = wmi.WMI()

 

filename = r"c:\temp\temp.txt"

process = c.Win32_Process

process_id, result = process.Create(CommandLine="notepad.exe " + filename)

watcher = c.watch_for(

  notification_type="Deletion",

  wmi_class="Win32_Process",

  delay_secs=1,

  ProcessId=process_id

)

 

watcher()

print "This is what you wrote:"

print open(filename).read()

监视新的打印任务

 

?

1

2

3

4

5

6

7

8

9

10

11

12

import wmi

c = wmi.WMI()

 

print_job_watcher = c.Win32_PrintJob.watch_for(

  notification_type="Creation",

  delay_secs=1

)

 

while 1:

  pj = print_job_watcher()

  print "User %s has submitted %d pages to printer %s" % \

    (pj.Owner, pj.TotalPages, pj.Name)

重启远程机器 

注:要对远程系统进行这样的操作,WMI脚本必须具有远程关机(RemoteShutdown)的权限,也就是说你必须在连接别名中进行指定。WMI构造器允许你传入一个完整的别名,或者是指定你需要的那一部分。使用wmi.WMI.__init__的帮助文档可以找到更多相关内容。

 

?

1

2

3

4

5

6

import wmi

# other_machine = "machine name of your choice"

c = wmi.WMI(computer=other_machine, privileges=["RemoteShutdown"])

 

os = c.Win32_OperatingSystem(Primary=1)[0]

os.Reboot()

对于启用IP的网卡显示其IP和MAC地址

 

?

1

2

3

4

5

6

7

8

import wmi

c = wmi.WMI()

 

for interface in c.Win32_NetworkAdapterConfiguration(IPEnabled=1):

  print interface.Description, interface.MACAddress

  for ip_address in interface.IPAddress:

    print ip_address

  print

查看自启动项

?

1

2

3

4

5

import wmi

c = wmi.WMI()

 

for s in c.Win32_StartupCommand():

  print "[%s] %s <%s>" %(s.Location, s.Caption, s.Command)

监视事件日志中的错误信息

?

1

2

3

4

5

6

7

8

9

10

11

12

import wmi

c = wmi.WMI(privileges=["Security"])

 

watcher = c.watch_for(

  notification_type="Creation",

  wmi_class="Win32_NTLogEvent",

  Type="error"

)

while 1:

  error = watcher()

  print "Error in %s log: %s" %(error.Logfile, error.Message)

  # send mail to sysadmin etc.

列出注册表子键 

注:本例及以下几例使用了Registry()这个方便的函数,此函数是早期加入到wmi包的,它等效于:

?

1

2

import wmi

r = wmi.WMI(namespace="DEFAULT").StdRegProv

?

1

2

3

4

5

6

7

8

9

10

import _winreg

import wmi

 

r = wmi.Registry()

result, names = r.EnumKey(

  hDefKey=_winreg.HKEY_LOCAL_MACHINE,

  sSubKeyName="Software"

)

for key in names:

  print key

增加一个新的注册表子键

?

1

2

3

4

5

6

7

8

import _winreg

import wmi

 

r = wmi.Registry()

result, = r.CreateKey(

  hDefKey=_winreg.HKEY_LOCAL_MACHINE,

  sSubKeyName=r"Software\TJG"

)

增加一个新的注册表键值

?

1

分割线

感谢打赏
江西数库信息技术有限公司
YWSOS.COM 平台代运维解决方案
 评论
 发表评论
姓   名:

Powered by AKCMS