如何以编程方式启用/禁用网络接口? (Windows XP)

问题描述:

我需要在Windows XP中启用/禁用完全来自脚本的网络接口。我正在寻找一个python解决方案,但任何一般的方法(例如WMI,一些命令行,netsh,一些窗口调用)都是受欢迎的,并且会进行调整。谢谢。如何以编程方式启用/禁用网络接口? (Windows XP)

使用netsh接口 使用一套接口[名称=] IFNAME [管理=]启用|禁用 [连接=]连| DISCONNECTED [NEWNAME =]新名称]

尝试,包括里面的一切的外括号: netsh接口设置接口名称= “thename” 管理=禁用连接= DISCONNECTED NEWNAME = “thename”

又见这个MS KB页:http://support.microsoft.com/kb/262265/ 你可以按照要么他们的建议。 要禁用适配器,您需要确定引用硬件设备的方法。如果计算机上没有多个具有相同名称的适配器,则可能会脱离接口的说明(或PCI ID工作正常)。之后,使用devcon(disable | enable)。 Devcon是设备管理器的附加控制台界面。

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

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

它似乎不适用于网络接口。 “devcon enable wifi”,其中wifi是无线接口的名称回复:“没有设备启用。” – tzot 2008-09-17 14:35:55

这是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 interface set interface DISABLED

+0

我试过了netsh暗示的所有组合: `netsh interface set interface [name =] wifi [admin =] DISABLED`带有“参数不正确”或“一个或多个基本参数不正确”消息。 – tzot 2008-09-17 14:52:13

+0

wmi不适用于xp – 2012-06-01 13:29:37

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

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

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

+0

这确实是一个很好的起点。我在做这个工作。谢谢。 – tzot 2008-09-17 15:15:14

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

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

这是翻译,AFAIU,以通用WMI错误0x80041001。可能是权限。

+0

很显然,我是以本地管理员组的成员身份运行它,并且该计算机不是域的一部分。 – tzot 2008-09-17 15:17:45

我在互联网上发现了这个.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