Pergunta

Eu preciso ativar / desativar completamente interfaces de rede de um script no Windows XP. Eu estou procurando uma solução python, mas qualquer maneira geral (por exemplo WMI, alguns de linha de comando à la netsh, algumas janelas chamada) é bem-vinda e será ajustado. Obrigado.

Foi útil?

Solução

Usando a interface netsh interface de conjunto de Uso [nome =] ifname [[Admin =] enabled | DEFICIENTES [Ligar =] CONECTADO | DISCONNECTED [Newname =] NewName]

Tente incluindo tudo dentro dos colchetes exteriores: netsh interface nome da interface set = "thename" admin = disabled connect = DISCONNECTED newname = "thename"

Veja também esta página MS KB: http://support.microsoft.com/kb/262265/ Você poderia seguir qualquer uma das suas sugestões. Para desabilitar o adaptador, você vai precisar para determinar uma forma de referenciar o dispositivo de hardware. Se não haverá várias placas com o mesmo nome no computador, você poderia ir para fora da Descrição para a interface (ou PCI ID funciona bem). Depois disso, usando devcon (disable | ativar). Devcon é um add-on interface do console para o Gerenciador de Dispositivos.

Outras dicas

Até agora eu encontrei a seguinte solução Python:

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

que é traduzido, AFAIU, ao genérico 0x80041001 erro WMI. Poderia ser permissões.

Eu encontrei este script .VBS na internet. Tem a vantagem legal de realmente trabalhar em máquinas onde eu não consigo NETSH ao trabalho para esse fim.

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

Eu não consigo encontrar qualquer API básica para controlar as interfaces na MSDN, além do RAS API 's, mas eu não acho que eles se aplicam a conexões não-discadas. Como você sugere-se, netsh pode ser uma opção, supostamente também tem uma interface de programação: http://msdn.microsoft.com/en-us/library/ms708353 (VS.85) .aspx

Se você quer ser pura Python, talvez você possa abrir um conjunto de tubos para se comunicar com um processo netsh.

este é 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

Isso vai ter todas as interfaces no seu computador. Então você pode fazer netsh para desativá-lo.

set interface netsh interface DEFICIENTES

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top