Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 574
tzot Avatar asked Sep 17 '08 14:09

tzot


People also ask

How do I enable and disable Ethernet network interface?

To disconnect the network connection: open “Network adapters“, right-click on the one you want to disconnect, and click on “Disable device“. In the window that appears, click on “Yes.” To enable a network connection: open “Network adapters“, right-click on the one you want to enable, and click on “Enable device“.


2 Answers

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.

like image 144
Kris Kumler Avatar answered Sep 19 '22 05:09

Kris Kumler


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.

like image 21
tzot Avatar answered Sep 19 '22 05:09

tzot