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.
views:
13260answers:
7The 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)
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
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.
You may need to use WMI. This may serve as a good starting point: http://msdn.microsoft.com/en-us/library/aa394595.aspx
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.
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.
(en)Thanks for ideas. I made little script collection about enable/disable network interfaces, ICS, NAT and dial-up.
(ge)Vielen Dank für die Tipps. Ich habe ein paar Scripts geschrieben rund um die Themen enable/disable von LAN Interfaces, ICS, NAT und Dial-up.
Enjoy X-INetConnect 2.0 -> http://x-event.designo.ch/init/?q=ge/node/392
Kind regard, steve