-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (53 loc) · 2.01 KB
/
main.py
File metadata and controls
63 lines (53 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import time
import subprocess
def get_current_wifi():
"""Get the current Wi-Fi SSID"""
try:
# Run netsh command to get current wifi info
result = subprocess.check_output('netsh wlan show interfaces', shell=True, text=True)
for line in result.split('\n'):
if "SSID" in line and "BSSID" not in line:
ssid = line.split(':')[1].strip()
return ssid if ssid else None
return None
except subprocess.CalledProcessError:
return None
def toggle_wifi():
"""Turn Wi-Fi off and on"""
# Disable Wi-Fi
os.system('netsh interface set interface "Wi-Fi" disable')
time.sleep(5) # Wait for 5 seconds
# Enable Wi-Fi
os.system('netsh interface set interface "Wi-Fi" enable')
time.sleep(5) # Wait for 5 seconds
def connect_to_wifi(ssid):
"""Connect to the specified Wi-Fi"""
os.system(f'netsh wlan connect name="{ssid}" ssid="{ssid}"')
time.sleep(5) # Wait for connection to establish
def main():
# Ask user for the target Wi-Fi name
TARGET_SSID = input("Enter the Wi-Fi name you want to stay connected to: ").strip()
if not TARGET_SSID:
print("No Wi-Fi name provided. Exiting...")
return
print(f"Monitoring Wi-Fi connection. Target network: {TARGET_SSID}")
while True:
current_ssid = get_current_wifi()
if current_ssid is None:
print("Wi-Fi is disconnected. Attempting to reconnect to target network...")
toggle_wifi()
connect_to_wifi(TARGET_SSID)
elif current_ssid != TARGET_SSID:
print(f"Connected to {current_ssid} instead of {TARGET_SSID}. Switching back...")
toggle_wifi()
connect_to_wifi(TARGET_SSID)
else:
print(f"Connected to {TARGET_SSID} - All good!")
# Check every 10 seconds
time.sleep(10)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nScript stopped by user.")