Domanda

Devo abilitare/disabilitare completamente le interfacce di rete da uno script in Windows XP.Sto cercando una soluzione Python, ma qualsiasi soluzione generale (ad esempio WMI, qualche riga di comando à la netsh, qualche chiamata Windows) è benvenuta e verrà modificata.Grazie.

È stato utile?

Soluzione

Utilizzando l'interfaccia set di utilizzo dell'interfaccia netsh [name =] ifname [[admin =] abilitato | disabilitato [connect =] connesso | disconnesso [newname =] newName

Prova a includere tutto all'interno delle parentesi esterne:netsh interfaccia set nome interfaccia="thename" admin=disabilitato connect=DISCONNECTED newname="thename"

Vedi anche questa pagina MS KB: http://support.microsoft.com/kb/262265/Potresti seguire uno dei loro suggerimenti.Per disabilitare l'adattatore, sarà necessario determinare un modo per fare riferimento al dispositivo hardware.Se non ci sono più adattatori con lo stesso nome sul computer, potresti uscire dalla Descrizione dell'interfaccia (o l'ID PCI funziona bene).Successivamente, utilizzando devcon (disable|enable).Devcon è un'interfaccia console aggiuntiva per Gestione dispositivi.

Altri suggerimenti

Finora ho trovato la seguente soluzione Python:

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

che viene tradotto, AFAIU, nell'errore WMI generico 0x80041001.Potrebbero essere i permessi.

Ho trovato questo script .VBS su Internet.Ha il vantaggio interessante di funzionare effettivamente su macchine su cui non riesco a far funzionare NETSH per questo scopo.

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

Non riesco a trovare alcuna API di base per il controllo delle interfacce su MSDN, a parte le API RAS, ma non penso che si applichino alle connessioni non dialup.Come suggerisci tu stesso, netsh potrebbe essere un'opzione, presumibilmente ha anche un'interfaccia programmatica: http://msdn.microsoft.com/en-us/library/ms708353(VS.85).aspx

Se vuoi essere puro Python, puoi forse aprire una serie di pipe per comunicare con un processo netsh.

questo è 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

Ciò otterrà tutte le interfacce sul tuo computer.Quindi puoi eseguire netsh per disabilitarlo.

Interfaccia interfaccia netsh interfaccia disabilitata

IL devcon lo strumento può controllare la scheda NIC, ma non direttamente l'interfaccia.È una versione da riga di comando dell'applet Gestione dispositivi.

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

Potrebbe essere necessario utilizzare WMI.Questo può servire come buon punto di partenza:http://msdn.microsoft.com/en-us/library/aa394595.aspx

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top