Last active
July 18, 2023 04:06
-
-
Save dcinzona/31d07a7707b2edd8db900333c37af667 to your computer and use it in GitHub Desktop.
connection-check
This file contains hidden or 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 os | |
import time | |
def write_to_log(log_message): | |
with open('network_status.log', 'a') as log_file: | |
log_file.write(log_message + '\n') | |
def is_connected(): | |
if os.name == 'nt': | |
return is_connected_win() | |
else: | |
return is_connected_unix() | |
def is_connected_win(): | |
# This command is different if on unix / mac | |
response = os.system("ping -n 1 8.8.8.8 >nul 2>nul") | |
return response == 0 | |
def is_connected_unix(): | |
response = os.system("ping -c 1 8.8.8.8 > /dev/null 2>&1") | |
return response == 0 | |
def main(): | |
wasConnected = True | |
while True: | |
if not is_connected(): | |
if wasConnected: | |
log_message = f"Network disconnected at {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}" | |
print(log_message) | |
write_to_log(log_message) | |
wasConnected = False | |
else: | |
if not wasConnected: | |
log_message = f"Network reconnected at {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}" | |
print(log_message) | |
write_to_log(log_message) | |
wasConnected = True | |
time.sleep(1) | |
if __name__ == "__main__": | |
print("Starting connectivity check...") | |
main() |
This file contains hidden or 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 psutil | |
from socket import AddressFamily | |
import time | |
from colorama import Fore, init | |
INTERESTED_INTERFACES = {'Ethernet 2', 'Wi-Fi 2', 'CloudflareWARP'} | |
def get_network_info(): | |
net_if_addrs = psutil.net_if_addrs() | |
return {k: [addr for addr in v if addr.family in [AddressFamily.AF_INET]] for k, v in net_if_addrs.items()} # if k in INTERESTED_INTERFACES} | |
init(autoreset=True) | |
# Initialize previous state | |
previous_state = get_network_info() | |
while True: | |
# Get the current state | |
current_state = get_network_info() | |
# Check for changes in the network interfaces | |
for interface in current_state.keys(): | |
# if interface not in ['Ethernet 2', 'Wi-Fi 2', 'CloudflareWARP']: | |
# continue | |
if interface not in previous_state.keys(): | |
print(Fore.GREEN + f"{interface} CONNECTED at {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} with IP {(current_state[interface][0]).address}") | |
for interface in previous_state.keys(): | |
# if interface not in ['Ethernet 2', 'Wi-Fi 2', 'CloudflareWARP']: | |
# continue | |
if interface not in current_state.keys(): | |
print(Fore.RED + f"{interface} DISCONNECTED at {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}") | |
# Save the current state as the previous state for the next iteration | |
previous_state = current_state | |
# Wait for 1 second before checking again | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment