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

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.

Asked by: Marcus349 | Posted: 28-01-2022

Answer 1

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: 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.

Answered by: Kellan557 | Posted: 01-03-2022

Answer 2

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)

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

Answered by: Gianna121 | Posted: 01-03-2022

Answer 3

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 = "Network Connections" then 
        set oNetConnections = folderitem.getfolder: exit for 
end if 

if oNetConnections is nothing then 
msgbox "Couldn't find 'Network Connections' folder" 
end if 

set oLanConnection = nothing 
for each folderitem in oNetConnections.items 
if lcase( = lcase(sConnectionName) then 
set oLanConnection = folderitem: exit for 
end if 

if oLanConnection is nothing then 
msgbox "Couldn't find '" & sConnectionName & "' item" 
end if 

bEnabled = true 
set oEnableVerb = nothing 
set oDisableVerb = nothing 
s = "Verbs: " & vbcrlf 
for each verb in oLanConnection.verbs 
s = s & vbcrlf & 
if = sEnableVerb then 
set oEnableVerb = verb 
bEnabled = false 
end if 
if = sDisableVerb then 
set oDisableVerb = verb 
end if 

'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 
' oLanConnection.invokeverb sEnableVerb 
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

Answered by: Adelaide362 | Posted: 01-03-2022

Answer 4

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:

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

Answered by: Hailey853 | Posted: 01-03-2022

Answer 5

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
         Next os

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

netsh interface set interface DISABLED

Answered by: Alford754 | Posted: 01-03-2022

Answer 6

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)

Answered by: Kelsey677 | Posted: 01-03-2022

Answer 7

You may need to use WMI. This may serve as a good starting point:

Answered by: Charlie215 | Posted: 01-03-2022

Similar questions

python - Programmatically enable/disable Bluetooth profiles

I'm running Rasbian Jessie Lite on Raspberry Pi 3 with a USB/Bluetooth dongle (blueZ) 5.4. The /etc/bluetooth/main.conf has Class = 0x0c0408. I have a Qt5 application which enables the Bluetooth device and accepts any incoming pairing requests. I can successfully connect from my smartphone to all enabled Bluetooth profiles: A2DP and HFP. Now I want to let the users select which profile...

python - Programmatically launching standalone Adobe flashplayer on Linux/X11

The standalone flashplayer takes no arguments other than a .swf file when you launch it from the command line. I need the player to go full screen, no window borders and such. This can be accomplished by hitting ctrl+f once the program has started. I want to do this programmatically as I need it to launch into full screen without any human interaction. My guess is that I need to some how get a handle to the window ...

python - How to create a picture with animated aspects programmatically

Background I have been asked by a client to create a picture of the world which has animated arrows/rays that come from one part of the world to another. The rays will be randomized, will represent a transaction, will fade out after they happen and will increase in frequency as time goes on. The rays will start in one country's boundary and end in another's. As each animated transaction happens a conti...

python - How do I use genshi.builder to programmatically build an HTML document?

I recently discovered the genshi.builder module. It reminds me of Divmod Nevow's Stan module. How would one use genshi.builder.tag to build an HTML document with a particular doctype? Or is this even a good thing to do? If not, what is the right way?

python - How do you programmatically reorder children of an ATFolder subclass?

I have Plone product that uses a custom folder type for containing a set of custom content objects. The folder type was created by subclassing BaseFolder and it has a schema with a couple of text fields. Currently, when custom objects are added to the custom folder, the objects are sorted alphabetically by their id. How can I override this behavior and allow my users to sort the custom folders manually, say through the ...

How to get the python.exe location programmatically?

This question already has answers here:

python - Programmatically taking screenshots in windows without the application noticing

This question already has answers here:

python - Discovering public IP programmatically

I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results) Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it? P.S. I'm planning on writing a short python script to do it

python - Free word list for use programmatically?

python - Best way to programmatically create image

I'm looking for a way to create a graphics file (I don't really mind the file type, as they are easily converted). The input would be the desired resolution, and a list of pixels and colors (x, y, RGB color). Is there a convenient python library for that? What are the pros\cons\pitfalls?

python - Scrolling QGraphicsView programmatically

I've want to implement a scroll/pan-feature on a QGraphicsView in my (Py)Qt application. It's supposed to work like this: The user presses the middle mouse button, and the view scrolls as the user moves the mouse (this is quite a common feature). I tried using the scroll() method inherited from QWidget. However, this somehow moves the view instead - scrollbars and all. See picture. So, given that this is not the wa...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python