Created
September 21, 2022 17:52
-
-
Save secemp9/b54a4d3c66adf735d7b11a31f325d2cf to your computer and use it in GitHub Desktop.
restart wifi hotspot on windows 10
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
import subprocess | |
import time | |
info = subprocess.STARTUPINFO() | |
info.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW | |
info.wShowWindow = subprocess.SW_HIDE | |
def run(cmd): | |
completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True, startupinfo=info) | |
return completed | |
def run2(x): | |
hello_info2 = run(x) | |
if hello_info2.returncode != 0: | |
print("An error occurred: %s", hello_info2.stderr) | |
else: | |
hello_info2 = hello_info2.stdout.rstrip().decode() | |
return hello_info2 | |
if __name__ == '__main__': | |
base_command = """ | |
$connectionProfile = [Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile() | |
$tetheringManager = [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile($connectionProfile) | |
""" | |
check_hotspot = """ | |
$tetheringManager.TetheringOperationalState | |
""" | |
check_hotspot = base_command + check_hotspot | |
turn_on = """ | |
Await ($tetheringManager.StartTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult]) | |
""" | |
turn_on = base_command + turn_on | |
turn_off = """ | |
Await ($tetheringManager.StopTetheringAsync()) ([Windows.Networking.NetworkOperators.NetworkOperatorTetheringOperationResult]) | |
""" | |
turn_off = base_command + turn_off | |
while 1: | |
if run2(check_hotspot) == "On": | |
time.sleep(15) | |
else: | |
run2(turn_on) | |
time.sleep(15) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment