如何在python中使用WMI连接到VM?

时间:2015-06-27 17:42:14

标签: python networking wmi virtual-machine remote-access

我正在尝试在python中创建一个脚本,该脚本获取有关我网络中机器的几条信息(例如CPU名称,网络适配器)。

该脚本目前正在我的计算机上使用wmi.WMI()(或wmi.WMI('localhost'))进行连接。

但现在我想看看它是否适用于其他机器。为此,我安装了VMWare并设置了一台虚拟机(运行Windows XP)。我想知道如何连接它。

我读过你可以简单地使用wmi.WMI([machine name or IP])但是加入IP ipconfig给我似乎不起作用。我收到错误The RPC server is unavailable

是的,有人可以帮帮我吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

One thing you can do that I found to be helpful was use a try...except statement inside a while True loop. That will repeatedly force WMI to connect to the machine and only break once the connection has been established. For example, in the case of the RPC server is unavailable error:

while True:
   try:
     comm = wmi.WMI([servername] user=[username] password=[password])
   except wmi.x_wmi:
     continue
   else:
     break

That should help out a little.