2008-09-17 95 views

回答

9

使用netsh接口 使用一套接口[名稱=] IFNAME [管理=]啓用|禁用 [連接=]連| DISCONNECTED [NEWNAME =]新名稱]

嘗試,包括裏面的一切的外括號: netsh接口設置接口名稱= 「thename」 管理=禁用連接= DISCONNECTED NEWNAME = 「thename」

又見這個MS KB頁:http://support.microsoft.com/kb/262265/ 你可以按照要麼他們的建議。 要禁用適配器,您需要確定引用硬件設備的方法。如果計算機上沒有多個具有相同名稱的適配器,則可能會脫離接口的說明(或PCI ID工作正常)。之後,使用devcon(disable | enable)。 Devcon是設備管理器的附加控制檯界面。

0

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

0

這是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

1

我似乎無法找到任何基本的API,用於控制在MSDN上的接口,除了RAS API的,但我不認爲他們適用於非撥號連接。正如你建議你自己,netsh可能是一個選項,據說它也有一個編程接口:http://msdn.microsoft.com/en-us/library/ms708353(VS.85).aspx

如果你想成爲純粹的Python,你可以打開一組管道來與netsh進程通信。

6

到目前爲止,我已經找到了以下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

2

我在互聯網上發現了這個.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 
相關問題