我需要通过 Windows XP 中的脚本完全启用/禁用网络接口。我正在寻找一个 python 解决方案,但任何通用方式(例如 WMI、一些命令行 à la netsh、一些 Windows 调用)都是受欢迎的,并将进行调整。谢谢。

有帮助吗?

解决方案

使用netsh接口用法集界面[name =] ifname [[admin =]启用| disabled [connect =]连接|脱连接[newname =] newname

尝试将所有内容包含在外括号内:netsh 接口设置接口名称=“名称”管理=禁用连接=断开新名称=“名称”

另请参阅此 MS 知识库页面: http://support.microsoft.com/kb/262265/您可以遵循他们的任何建议。要禁用适配器,您需要确定引用硬件设备的方法。如果计算机上不存在多个同名的适配器,您可能会忽略接口的描述(或者 PCI ID 效果很好)。之后,使用 devcon(禁用|启用)。Devcon 是设备管理器的附加控制台界面。

其他提示

到目前为止,我已经找到了以下Python解决方案:

>>> import wmi; c=wmi.WMI()
>>> o=c.query("select * from Win32_NetworkAdapter where NetConnectionID='wifi'")[0]
>>> o.EnableDevice(1)
(-2147217407,)

AFAIU 将其转换为通用 WMI 错误 0x80041001。可能是权限。

我在互联网上找到了这个 .VBS 脚本。它有一个很酷的优势,那就是在我无法让 NETSH 实现此目的的机器上实际工作。

Const ssfCONTROLS = 3 

sConnectionName = "Local Area Connection" 

sEnableVerb = "En&able" 
sDisableVerb = "Disa&ble" 

set shellApp = createobject("shell.application") 
set oControlPanel = shellApp.Namespace(ssfCONTROLS) 

set oNetConnections = nothing 
for each folderitem in oControlPanel.items 
  if folderitem.name = "Network Connections" then 
        set oNetConnections = folderitem.getfolder: exit for 
end if 
next 

if oNetConnections is nothing then 
msgbox "Couldn't find 'Network Connections' folder" 
wscript.quit 
end if 

set oLanConnection = nothing 
for each folderitem in oNetConnections.items 
if lcase(folderitem.name) = lcase(sConnectionName) then 
set oLanConnection = folderitem: exit for 
end if 
next 

if oLanConnection is nothing then 
msgbox "Couldn't find '" & sConnectionName & "' item" 
wscript.quit 
end if 

bEnabled = true 
set oEnableVerb = nothing 
set oDisableVerb = nothing 
s = "Verbs: " & vbcrlf 
for each verb in oLanConnection.verbs 
s = s & vbcrlf & verb.name 
if verb.name = sEnableVerb then 
set oEnableVerb = verb 
bEnabled = false 
end if 
if verb.name = sDisableVerb then 
set oDisableVerb = verb 
end if 
next 

'debugging displays left just in case... 
' 
'msgbox s ': wscript.quit 
'msgbox "Enabled: " & bEnabled ': wscript.quit 

'not sure why, but invokeverb always seemed to work 
'for enable but not disable. 
' 
'saving a reference to the appropriate verb object 
'and calling the DoIt method always seems to work. 
' 
if bEnabled then 
' oLanConnection.invokeverb sDisableVerb 
oDisableVerb.DoIt 
else 
' oLanConnection.invokeverb sEnableVerb 
oEnableVerb.DoIt 
end if 

'adjust the sleep duration below as needed... 
' 
'if you let the oLanConnection go out of scope 
'and be destroyed too soon, the action of the verb 
'may not take... 
' 
wscript.sleep 1000

除了 RAS API 之外,我似乎在 MSDN 上找不到任何用于控制接口的基本 API,但我认为它们不适用于非拨号连接。正如您自己建议的那样,netsh 可能是一个选择,据说它还有一个编程接口: http://msdn.microsoft.com/en-us/library/ms708353(VS.85).aspx

如果你想成为纯粹的Python,你也许可以打开一组管道来与netsh进程进行通信。

这是VB.Net

Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId IS NOT NULL")
         Dim searcher As New ManagementObjectSearcher(scope, objectQuery)
         Dim os As ManagementObject
         Dim moColl As ManagementObjectCollection = searcher.Get()
         Dim _list As String = ""
         For Each os In moColl
             Console.WriteLine(os("NetConnectionId"))
         Next os

这将获得您计算机上的所有接口。然后你可以执行 netsh 来禁用它。

Netsh接口设置接口禁用

开发者大会 工具可以控制网卡,但不能直接控制接口。它是设备管理器小程序的命令行版本。

devcon disable (id or portion of name)
devcon enable (id or portion of name)

您可能需要使用 WMI。这可以作为一个很好的起点:http://msdn.microsoft.com/en-us/library/aa394595.aspx

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top