从WMI com对象调用Win32_NetworkAdapterConfiguration例程

时间:2011-11-26 10:13:02

标签: python

提前感谢您的帮助。我正在尝试使用win32com模块设置网络接口的IP地址,但无法这样做。我已经尝试了很多搜索,但无法得到问题的答案。这是我正在运行的代码: import win32com.client

obj = win32com.client.Dispatch("WbemScripting.SWbemLocator")

wmobj = obj.ConnectServer("localhost","root\cimv2")

nobj = wmobj.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")

for n in nobj:

print n.Caption

n.SetMTU('9000')

print n.Caption n.SetMTU('9000')

当我运行此代码时,它出错并出现以下错误:

追踪(最近一次通话):   文件“”,第3行,in     n.SetMTU( '9000')   文件“C:\ Python27 \ lib \ site-packages \ win32com \ client \ dynamic.py”,第505行, getattr     ret = self。 oleobj .Invoke(retEntry.dispid,0,invoke_type,1) com_error:(-2147352567,'异常发生。',(0,u'SWbemObjectEx',u'无效方法',无,0,-2147217362),无)

我做了一些调试,发现我可以访问Win32Networking类的任何变量,但每当我尝试调用该类的任何方法时,它都会返回同样的错误。

3 个答案:

答案 0 :(得分:2)

我没有太多使用win32com的经验,但可能没有实现SetMTU方法。根据{{​​3}}的MSDN文档,该方法“不受支持”。它在XP中失败了。

请注意,使用win32com,只需访问属性即可调用它:

>>> import win32com.client
>>> wmobj = obj.ConnectServer("localhost","root\cimv2")
>>> nobj = wmobj.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
>>> n = nobj[10]  #my wireless interface
>>> n.ReleaseDHCPLease  #invoked
0
>>> n.RenewDHCPLease
0

尝试正常调用它将最终尝试调用整数返回值,这会引发TypeError。但是,您可以先将这样的方法包装起来,使其成为普通的Python调用:

>>> n._FlagAsMethod('ReleaseDHCPLease')
>>> n._FlagAsMethod('RenewDHCPLease')
>>> n.ReleaseDHCPLease()
0
>>> n.RenewDHCPLease()
0

编辑:

在上面链接的页面的用户贡献区域中,搜索必须从类中访问的静态方法列表,包括SetMTU。以下是如何获得课程:

>>> NetAdapterConfig = wmobj.Get("Win32_NetworkAdapterConfiguration")
>>> NetAdapterConfig._FlagAsMethod('SetMTU')

有关返回值的含义,请参阅Win32_NetworkAdapterConfiguration class。虽然我真的不明白这个方法在静态环境中的作用。


以下是使用标准库winreg更新注册表的示例:

import winreg

nid = n.SettingID
MTU = 1500

path = r'SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\\'+ nid 
sam = winreg.KEY_ALL_ACCESS
adapter = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path, 0, sam)
winreg.SetValueEx(adapter, 'MTU', None, winreg.REG_DWORD, MTU)

答案 1 :(得分:1)

我遇到了类似的问题,并找到了一种方法来调用Win32_NetworkAdapterConfiguration对象的这些方法。这里我没有直接使用这些方法,而是将所有输入参数包装到SpawnInstance_中。它似乎有线但有效。

import win32com.client
objLocator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objService = objLocator.ConnectServer(".","root\cimv2")
nobj = objService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
obj = nobj[0]

## here is the strange part. 
obj_method = obj.Methods_("SetWINSServer")
obj_in_param = obj_method.inParameters.SpawnInstance_()
obj_in_param.WINSPrimaryServer = "127.0.0.1"
obj_in_param.WINSSecondaryServer = "127.0.0.2"
#invoke the SetWINServer method, and it worked.
obj.ExecMethod_("SetWINSServer", obj_in_param)

几乎所有这些Win32_NetworkAdapterConfiguration方法都可以像这样使用。 但是,“SetMTU”不能,也许是因为“不支持此方法”,我在Windows 2008R2中尝试但得到相同的错误。 它说这里不支持SetMTU。 http://msdn.microsoft.com/en-us/library/windows/desktop/aa393463(v=vs.85).aspx

答案 2 :(得分:0)

好吧,我刚试过FlagAsMethod

建议

对于Win32_Process类,例如:

proc10 = GetObject(pos).ExecQuery("Select * from Win32_Process")[10]
proc10._FlagAsMethod('GetOwner')
proc10.GetOwner()
# >> 0

但是,我期待domain\user等等。此外,我也能写

proc10.GetOwner(10,20,30)

但效果是一样的。

我发布了here一个适用于我案例的方法。我只是想知道SetMTU是否使用_FlagAsMethod提示获得了正确的值。

相关问题