Created
August 8, 2022 17:11
-
-
Save MisaghM/1b0aecf579ab46b399c9bdb9757d5dd7 to your computer and use it in GitHub Desktop.
Reset USB tethering network number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Reset USB tethering network number. | |
Removes the network profiles made by tethering each time. | |
Requires administrative privileges. | |
""" | |
import winreg | |
import re | |
PROFILES_PATH = r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\\' | |
network_rgx = re.compile(r'^Network( \d+)?$') | |
def del_profiles() -> bool: | |
any_deleted = False | |
try: | |
registry_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, PROFILES_PATH, 0, winreg.KEY_READ) | |
i = 0 | |
while i < winreg.QueryInfoKey(registry_key)[0]: | |
subkey_name = winreg.EnumKey(registry_key, i) | |
subreg_key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, PROFILES_PATH + subkey_name, 0, winreg.KEY_READ) | |
profile_name, _ = winreg.QueryValueEx(subreg_key, 'ProfileName') | |
description, _ = winreg.QueryValueEx(subreg_key, 'Description') | |
name_type, _ = winreg.QueryValueEx(subreg_key, 'NameType') | |
if name_type == 6 and description == 'Network' and re.match(network_rgx, profile_name): | |
winreg.DeleteKeyEx(subreg_key, '', winreg.KEY_WOW64_64KEY, 0) | |
print('Deleted:', profile_name) | |
any_deleted = True | |
else: | |
i += 1 | |
winreg.CloseKey(subreg_key) | |
winreg.CloseKey(registry_key) | |
except OSError as ex: | |
raise SystemExit(ex) from ex | |
return any_deleted | |
def main() -> None: | |
if del_profiles(): | |
print('\nSuccessfully deleted wired network profiles.') | |
else: | |
print('No wired network profiles found.') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment