From 2718f80f764f956cb0df173fc6c75dd2d94758bd Mon Sep 17 00:00:00 2001 From: Badis Date: Sun, 10 Nov 2024 11:57:38 +0100 Subject: [PATCH 1/5] Add reconnection attempts for Wi-Fi --- Auto WiFi Check/wifi_checker.py | 27 +++++++++++++++++++++++---- Auto WiFi Check/wifi_status_log.txt | 0 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 Auto WiFi Check/wifi_status_log.txt diff --git a/Auto WiFi Check/wifi_checker.py b/Auto WiFi Check/wifi_checker.py index c2c3048c..a9f90673 100644 --- a/Auto WiFi Check/wifi_checker.py +++ b/Auto WiFi Check/wifi_checker.py @@ -20,12 +20,31 @@ def job(): if subprocess.call("netsh interface set interface Wi-Fi enabled") == 0: print("WiFi is enabled and connected to internet") hostname = "www.google.com" - response = subprocess.call("ping -n 1 " + hostname) + response = subprocess.call(f"ping -n 1 {hostname}") # Using f-string for cleaner string formatting if response == 1: print("Your Connection is not working") - disable() - time.sleep(1) - enable() + + attempt_counter = 0 + max_attempts = 3 + + while attempt_counter < max_attempts: + print(f"Attempt {attempt_counter} to reconnect...") + + disable() + time.sleep(1) + enable() + + time.sleep(5) + + response = subprocess.call(f"ping -n 1 {hostname}") + if response == 0: + print("Reconnection successful!") + break + elif response == 1: + print(f"Reconnection attempt {attempt_counter} failed.") + + if attempt_counter == max_attempts and response != 0: + print(f"Failed to reconnect after {attempt_counter} attemps.") def is_admin(): try: diff --git a/Auto WiFi Check/wifi_status_log.txt b/Auto WiFi Check/wifi_status_log.txt new file mode 100644 index 00000000..e69de29b From 584146f4fe1e6f43f1f8be6064561293f99baf3d Mon Sep 17 00:00:00 2001 From: Badis <167994292+Badis213@users.noreply.github.com> Date: Sun, 10 Nov 2024 11:59:30 +0100 Subject: [PATCH 2/5] Delete Auto WiFi Check/wifi_status_log.txt --- Auto WiFi Check/wifi_status_log.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Auto WiFi Check/wifi_status_log.txt diff --git a/Auto WiFi Check/wifi_status_log.txt b/Auto WiFi Check/wifi_status_log.txt deleted file mode 100644 index e69de29b..00000000 From cadf13c957b6d7248b56492d3552af925caf1b5a Mon Sep 17 00:00:00 2001 From: Badis Date: Sun, 10 Nov 2024 12:12:03 +0100 Subject: [PATCH 3/5] Added Wi-Fi reconnection attempts with logging --- Auto WiFi Check/wifi_checker.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/Auto WiFi Check/wifi_checker.py b/Auto WiFi Check/wifi_checker.py index a9f90673..1232f02d 100644 --- a/Auto WiFi Check/wifi_checker.py +++ b/Auto WiFi Check/wifi_checker.py @@ -2,34 +2,44 @@ import subprocess import sys import time +import logging from datetime import datetime import schedule as sc +# Set up logging to log to a file with timestamps +logging.basicConfig(filename='wifi_status_log.txt', + level=logging.INFO, + format='%(asctime)s - %(message)s') def enable(): subprocess.call("netsh interface set interface Wi-Fi enabled") print("Turning On the laptop WiFi") + logging.info("WiFi enabled") def disable(): subprocess.call("netsh interface set interface Wi-Fi disabled") print("Turning Off the laptop WiFi") + logging.info("WiFi disabled") - - def job(): if subprocess.call("netsh interface set interface Wi-Fi enabled") == 0: print("WiFi is enabled and connected to internet") + logging.info("WiFi is enabled and connected to the internet.") + hostname = "www.google.com" response = subprocess.call(f"ping -n 1 {hostname}") # Using f-string for cleaner string formatting + if response == 1: print("Your Connection is not working") + logging.warning("WiFi connection not working, ping failed.") attempt_counter = 0 max_attempts = 3 while attempt_counter < max_attempts: print(f"Attempt {attempt_counter} to reconnect...") - + logging.info(f"Attempt {attempt_counter} to reconnect...") + disable() time.sleep(1) enable() @@ -39,12 +49,17 @@ def job(): response = subprocess.call(f"ping -n 1 {hostname}") if response == 0: print("Reconnection successful!") + logging.info("Reconnection successful!") break elif response == 1: print(f"Reconnection attempt {attempt_counter} failed.") + logging.warning(f"Reconnection attempt {attempt_counter} failed.") + attempt_counter += 1 + if attempt_counter == max_attempts and response != 0: - print(f"Failed to reconnect after {attempt_counter} attemps.") + print(f"Failed to reconnect after {attempt_counter} attempts.") + logging.error(f"Failed to reconnect after {attempt_counter} attempts.") def is_admin(): try: @@ -53,12 +68,10 @@ def is_admin(): return False if is_admin(): - # job() sc.every(50).seconds.do(job) else: ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1) - while True: sc.run_pending() - time.sleep(1) \ No newline at end of file + time.sleep(1) From 4ee8343591c02af48465f45604c561b47b633b59 Mon Sep 17 00:00:00 2001 From: Badis <167994292+Badis213@users.noreply.github.com> Date: Sun, 10 Nov 2024 12:15:59 +0100 Subject: [PATCH 4/5] Create wifi_status_log.txt --- Auto WiFi Check/wifi_status_log.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Auto WiFi Check/wifi_status_log.txt diff --git a/Auto WiFi Check/wifi_status_log.txt b/Auto WiFi Check/wifi_status_log.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/Auto WiFi Check/wifi_status_log.txt @@ -0,0 +1 @@ + From 234028de507c257837bde2343d7eadf639f3109e Mon Sep 17 00:00:00 2001 From: Badis Date: Mon, 18 Nov 2024 19:06:16 +0100 Subject: [PATCH 5/5] Auto creation of log txt file --- Auto WiFi Check/wifi_checker.py | 69 ++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/Auto WiFi Check/wifi_checker.py b/Auto WiFi Check/wifi_checker.py index 1232f02d..e0c20b9b 100644 --- a/Auto WiFi Check/wifi_checker.py +++ b/Auto WiFi Check/wifi_checker.py @@ -3,31 +3,50 @@ import sys import time import logging -from datetime import datetime import schedule as sc +# Create the log file if it doesn't already exist +LOG_FILE = "wifi_status_log.txt" +PING_HOST = "www.google.com" + +try: + with open(LOG_FILE, 'x') as file: + file.write("Logs:\n") + print(f"File '{LOG_FILE}' created successfully.") +except FileExistsError: + print(f"File '{LOG_FILE}' already exists.") + # Set up logging to log to a file with timestamps -logging.basicConfig(filename='wifi_status_log.txt', +logging.basicConfig(filename=LOG_FILE, level=logging.INFO, - format='%(asctime)s - %(message)s') + format='%(asctime)s - %(message)s', + filemode='a') # Append mode def enable(): - subprocess.call("netsh interface set interface Wi-Fi enabled") - print("Turning On the laptop WiFi") - logging.info("WiFi enabled") + try: + subprocess.call("netsh interface set interface Wi-Fi enabled", shell=True) + print("Turning On the laptop WiFi") + logging.info("WiFi enabled") + except Exception as e: + print(f"Failed to enable WiFi: {e}") + logging.error(f"Failed to enable WiFi: {e}") def disable(): - subprocess.call("netsh interface set interface Wi-Fi disabled") - print("Turning Off the laptop WiFi") - logging.info("WiFi disabled") + try: + subprocess.call("netsh interface set interface Wi-Fi disabled", shell=True) + print("Turning Off the laptop WiFi") + logging.info("WiFi disabled") + except Exception as e: + print(f"Failed to disable WiFi: {e}") + logging.error(f"Failed to disable WiFi: {e}") def job(): - if subprocess.call("netsh interface set interface Wi-Fi enabled") == 0: + try: + subprocess.call("netsh interface set interface Wi-Fi enabled", shell=True) print("WiFi is enabled and connected to internet") logging.info("WiFi is enabled and connected to the internet.") - hostname = "www.google.com" - response = subprocess.call(f"ping -n 1 {hostname}") # Using f-string for cleaner string formatting + response = subprocess.call(f"ping -n 1 {PING_HOST}", shell=True) if response == 1: print("Your Connection is not working") @@ -35,10 +54,10 @@ def job(): attempt_counter = 0 max_attempts = 3 - + while attempt_counter < max_attempts: - print(f"Attempt {attempt_counter} to reconnect...") - logging.info(f"Attempt {attempt_counter} to reconnect...") + print(f"Attempt {attempt_counter + 1} to reconnect...") + logging.info(f"Attempt {attempt_counter + 1} to reconnect...") disable() time.sleep(1) @@ -46,25 +65,29 @@ def job(): time.sleep(5) - response = subprocess.call(f"ping -n 1 {hostname}") + response = subprocess.call(f"ping -n 1 {PING_HOST}", shell=True) if response == 0: print("Reconnection successful!") logging.info("Reconnection successful!") break - elif response == 1: - print(f"Reconnection attempt {attempt_counter} failed.") - logging.warning(f"Reconnection attempt {attempt_counter} failed.") + else: + print(f"Reconnection attempt {attempt_counter + 1} failed.") + logging.warning(f"Reconnection attempt {attempt_counter + 1} failed.") attempt_counter += 1 - if attempt_counter == max_attempts and response != 0: - print(f"Failed to reconnect after {attempt_counter} attempts.") - logging.error(f"Failed to reconnect after {attempt_counter} attempts.") + if attempt_counter == max_attempts and response != 0: + print(f"Failed to reconnect after {max_attempts} attempts.") + logging.error(f"Failed to reconnect after {max_attempts} attempts.") + except Exception as e: + print(f"Error during WiFi check: {e}") + logging.error(f"Error during WiFi check: {e}") def is_admin(): try: return ctypes.windll.shell32.IsUserAnAdmin() - except: + except Exception as e: + logging.error(f"Admin check failed: {e}") return False if is_admin():