How to programmatically enable/disable network interfaces? (Windows XP)
Asked Answered
S

7

13

I need to enable/disable completely network interfaces from a script in Windows XP. I'm looking for a python solution, but any general way (eg WMI, some command-line à la netsh, some windows call) is welcome and will be adjusted. Thanks.

Shaylynn answered 17/9, 2008 at 14:22 Comment(0)
D
11

Using the netsh interface Usage set interface [name = ] IfName [ [admin = ] ENABLED|DISABLED [connect = ] CONNECTED|DISCONNECTED [newname = ] NewName ]

Try including everything inside the outer brackets: netsh interface set interface name="thename" admin=disabled connect=DISCONNECTED newname="thename"

See also this MS KB page: http://support.microsoft.com/kb/262265/ You could follow either of their suggestions. For disabling the adapter, you will need to determine a way to reference the hardware device. If there will not be multiple adapters with the same name on the computer, you could possibly go off of the Description for the interface (or PCI ID works well). After that, using devcon (disable|enable). Devcon is an add-on console interface for the Device Manager.

Demy answered 17/9, 2008 at 19:20 Comment(3)
Including all options produces the following message: "Dedicated interfaces can not be connected, disconnected, enabled, or disabled." It should work, but it doesn't. Haven't tested on another computer, though; I will. Thanks.Shaylynn
I get the same strange problem. Netsh appears to not work as intended, on this Windows XP system I am trying to do this on.Replication
have you found a workaround for the 'dedicated interfaces cannot...' error?Crossbred
S
6

So far I've found the following Python solution:

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

which is translated, AFAIU, to the generic WMI error 0x80041001. Could be permissions.

Shaylynn answered 17/9, 2008 at 15:14 Comment(3)
Obviously I am running this as a member of the local Administrators group, and the computer is not part of a domain.Shaylynn
Traceback (most recent call last): File "C:\Python33\lib\site-packages\wmi.py", line 416, in call result = self.ole_object.ExecMethod_ (self.method.Name) File "<COMObject <unknown>>", line 3, in ExecMethod_ pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemObjectEx', 'Invalid method Parameter(s) ', None, 0, -2147217361), None)Anon
@HaifengLi: thanks, but I don't do Windows anymore, and neither the code above was tested on Python 3 (note the date of the answer).Shaylynn
R
2

I found this .VBS script on the internet. It has the cool advantage of actually working on machines where I cannot get NETSH to work for this purpose.

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
Replication answered 30/11, 2010 at 18:34 Comment(0)
S
1

I can't seem to find any basic API for controlling interfaces on MSDN, apart from the RAS API's, but I don't think they apply to non-dialup connections. As you suggest yourself, netsh might be an option, supposedly it also has a programmatic interface: http://msdn.microsoft.com/en-us/library/ms708353(VS.85).aspx

If you want to be pure Python, you can perhaps open a set of pipes to communicate with an netsh process.

Shrader answered 17/9, 2008 at 14:40 Comment(0)
A
0

The devcon tool can control the NIC, but not the interface directly. It's a command-line version of the Device Manager applet.

devcon disable (id or portion of name)
devcon enable (id or portion of name)
Avoirdupois answered 17/9, 2008 at 14:26 Comment(1)
It doesn't seem to work with network interfaces. "devcon enable wifi", where wifi is the name of the wireless interface replies: "No devices enabled."Shaylynn
D
0

this is 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

That will get all the interfaces on you computer. Then you can do netsh to disable it.

netsh interface set interface DISABLED

Deboer answered 17/9, 2008 at 14:35 Comment(1)
I've tried in the past all the combinations that netsh suggests: netsh interface set interface [name=]wifi [admin=]DISABLED with either "The parameter is incorrect" or "One or more essential parameters is incorrect" messages.Shaylynn
S
0

You may need to use WMI. This may serve as a good starting point: http://msdn.microsoft.com/en-us/library/aa394595.aspx

Swatter answered 17/9, 2008 at 14:52 Comment(1)
It's a good starting point indeed. I'm working on it. Thanks.Shaylynn

© 2022 - 2024 — McMap. All rights reserved.