2
2
import subprocess
3
3
import sys
4
4
import time
5
- from datetime import datetime
5
+ import logging
6
6
import schedule as sc
7
7
8
+ # Create the log file if it doesn't already exist
9
+ LOG_FILE = "wifi_status_log.txt"
10
+ PING_HOST = "www.google.com"
11
+
12
+ try :
13
+ with open (LOG_FILE , 'x' ) as file :
14
+ file .write ("Logs:\n " )
15
+ print (f"File '{ LOG_FILE } ' created successfully." )
16
+ except FileExistsError :
17
+ print (f"File '{ LOG_FILE } ' already exists." )
18
+
19
+ # Set up logging to log to a file with timestamps
20
+ logging .basicConfig (filename = LOG_FILE ,
21
+ level = logging .INFO ,
22
+ format = '%(asctime)s - %(message)s' ,
23
+ filemode = 'a' ) # Append mode
8
24
9
25
def enable ():
10
- subprocess .call ("netsh interface set interface Wi-Fi enabled" )
11
- print ("Turning On the laptop WiFi" )
26
+ try :
27
+ subprocess .call ("netsh interface set interface Wi-Fi enabled" , shell = True )
28
+ print ("Turning On the laptop WiFi" )
29
+ logging .info ("WiFi enabled" )
30
+ except Exception as e :
31
+ print (f"Failed to enable WiFi: { e } " )
32
+ logging .error (f"Failed to enable WiFi: { e } " )
12
33
13
34
def disable ():
14
- subprocess .call ("netsh interface set interface Wi-Fi disabled" )
15
- print ("Turning Off the laptop WiFi" )
16
-
35
+ try :
36
+ subprocess .call ("netsh interface set interface Wi-Fi disabled" , shell = True )
37
+ print ("Turning Off the laptop WiFi" )
38
+ logging .info ("WiFi disabled" )
39
+ except Exception as e :
40
+ print (f"Failed to disable WiFi: { e } " )
41
+ logging .error (f"Failed to disable WiFi: { e } " )
17
42
18
-
19
43
def job ():
20
- if subprocess .call ("netsh interface set interface Wi-Fi enabled" ) == 0 :
44
+ try :
45
+ subprocess .call ("netsh interface set interface Wi-Fi enabled" , shell = True )
21
46
print ("WiFi is enabled and connected to internet" )
22
- hostname = "www.google.com"
23
- response = subprocess .call ("ping -n 1 " + hostname )
47
+ logging .info ("WiFi is enabled and connected to the internet." )
48
+
49
+ response = subprocess .call (f"ping -n 1 { PING_HOST } " , shell = True )
50
+
24
51
if response == 1 :
25
52
print ("Your Connection is not working" )
26
- disable ()
27
- time .sleep (1 )
28
- enable ()
53
+ logging .warning ("WiFi connection not working, ping failed." )
54
+
55
+ attempt_counter = 0
56
+ max_attempts = 3
57
+
58
+ while attempt_counter < max_attempts :
59
+ print (f"Attempt { attempt_counter + 1 } to reconnect..." )
60
+ logging .info (f"Attempt { attempt_counter + 1 } to reconnect..." )
61
+
62
+ disable ()
63
+ time .sleep (1 )
64
+ enable ()
65
+
66
+ time .sleep (5 )
67
+
68
+ response = subprocess .call (f"ping -n 1 { PING_HOST } " , shell = True )
69
+ if response == 0 :
70
+ print ("Reconnection successful!" )
71
+ logging .info ("Reconnection successful!" )
72
+ break
73
+ else :
74
+ print (f"Reconnection attempt { attempt_counter + 1 } failed." )
75
+ logging .warning (f"Reconnection attempt { attempt_counter + 1 } failed." )
76
+
77
+ attempt_counter += 1
78
+
79
+ if attempt_counter == max_attempts and response != 0 :
80
+ print (f"Failed to reconnect after { max_attempts } attempts." )
81
+ logging .error (f"Failed to reconnect after { max_attempts } attempts." )
82
+ except Exception as e :
83
+ print (f"Error during WiFi check: { e } " )
84
+ logging .error (f"Error during WiFi check: { e } " )
29
85
30
86
def is_admin ():
31
87
try :
32
88
return ctypes .windll .shell32 .IsUserAnAdmin ()
33
- except :
89
+ except Exception as e :
90
+ logging .error (f"Admin check failed: { e } " )
34
91
return False
35
92
36
93
if is_admin ():
37
- # job()
38
94
sc .every (50 ).seconds .do (job )
39
95
else :
40
96
ctypes .windll .shell32 .ShellExecuteW (None , "runas" , sys .executable , " " .join (sys .argv ), None , 1 )
41
97
42
-
43
98
while True :
44
99
sc .run_pending ()
45
- time .sleep (1 )
100
+ time .sleep (1 )
0 commit comments