2

Is it possible to disable and enable network connection in Python in Windows 7? I saw a previous question about this here: How to programmatically enable/disable network interfaces? (Windows XP), but still couldn't find a solution! Please share the code with me! Martin's answer gave me this: b'Index Name

 \r\r\n0      WAN Miniport (SSTP)                                                   \r\r\n1      WAN Miniport (IKEv2)                                                  \r\r\n2      WAN Miniport (L2TP)                                                   \r\r\n3      WAN Miniport (PPTP)                                                   \r\r\n4      WAN Miniport (PPPOE)                                                  \r\r\n5      WAN Miniport (IPv6)                                                   \r\r\n6      WAN Miniport (Network Monitor)                                        \r\r\n7      Realtek RTL8102E/RTL8103E Family PCI-E Fast Ethernet NIC (NDIS 6.20)  \r\r\n8      WAN Miniport (IP)                                                     \r\r\n9      Microsoft ISATAP Adapter                                              \r\r\n10     RAS Async Adapter                                                     \r\r\n11     Atheros AR5007 802.11b/g WiFi Adapter                                 \r\r\n12     Teredo Tunneling Pseudo-Interface                                     \r\r\n13     Microsoft ISATAP Adapter #2                                           \r\r\n\r\r\n'

1 Answer 1

5

Taken from here:

You need to use subprocess to start the following command-line utilities:

Start elevated Command Prompt.

# Get NIC list and index number:
wmic nic get name, index

# Enable NIC with index number: (eg: 7)
wmic path win32_networkadapter where index=7 call enable

# Disable NIC with index number: (eg: 7)
wmic path win32_networkadapter where index=7 call disable

So in Python you would use something like

import subprocess
# get list of adapters and find index of adapter you want to disable.
subprocess.check_output('wmic nic get name, index')

To get the list of adapters, then run subprocess.check_output again for the other commands once you know the index. Also make sure you are running your python script as a privileged user.

Sign up to request clarification or add additional context in comments.

4 Comments

Updated my question. How is this used to disable network connection?
Make sure you strip the output: subprocess.check_output('wmic nic get name, index').strip() - this will remove the \n\n characters. Then your first character will be 0 which is the index. You should then be able to call wmic path win32_networkadapter where index=0 call disable
For some reason, .strip() isn't removing the '\r' characters!
So I need to find the network adapter in all that? How?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.