Last active
December 7, 2020 19:54
-
-
Save NFodrea/20be2344dc3f9f69426f02a5f2f07c7f to your computer and use it in GitHub Desktop.
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 | |
| import threading | |
| from datetime import datetime | |
| def updateLoop(): | |
| ip = "8.8.8.8" | |
| was_online = True | |
| down_timestamp = datetime.now() | |
| while True: | |
| now = datetime.now() | |
| timestamp = now.strftime("%d/%m/%Y %H:%M:%S") | |
| with open("results.txt", "a") as results_file: | |
| response = os.popen(f"ping {ip}").read() | |
| if "Received = 1" and "Approximate" in response: | |
| if was_online == False: | |
| print(f"Time came back online: {timestamp}") | |
| results_file.write(f"Time came back online: {timestamp} ") | |
| results_file.write( | |
| f"Time offline: {(datetime.now() - down_timestamp).total_seconds()}") | |
| was_online = True | |
| elif was_online == True and not "Received = 1" in response: | |
| print(f"Down {ip} Ping Unsuccessful") | |
| results_file.write( | |
| f"Internet outage timestamp: {timestamp} pinging google.com" + "\n") | |
| was_online = False | |
| down_timestamp = datetime.now() | |
| time.sleep(5) | |
| thread = threading.Thread(target=updateLoop) | |
| thread.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment