Skip to content

Instantly share code, notes, and snippets.

@the100rabh
Last active February 18, 2018 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the100rabh/ca854e0c20b0a8078e5bfa8ceb8c3f74 to your computer and use it in GitHub Desktop.
Save the100rabh/ca854e0c20b0a8078e5bfa8ceb8c3f74 to your computer and use it in GitHub Desktop.
It makes a CSV record of the connection status for tracking downtimes by ISP
import os
import datetime
def update_needed(status):
check_for = "UP"
if status == False:
check_for = "DOWN"
f = open("data.csv", "r")
lastLine = ""
currentLine = f.readline()
while currentLine:
lastLine = currentLine.strip()
currentLine = f.readline()
data = lastLine.split(',')
f.close()
if len(data) == 2:
if data[1] == check_for:
return False
return True
def record(status):
if update_needed(status):
f = open("data.csv","a+")
date_str = datetime.datetime.now().strftime("%d-%m-%Y %I:%M:%S %p")
f.seek(0, 2)
f.write(date_str)
f.write(',')
if status == True:
f.write('UP\n')
else:
f.write("DOWN\n")
f.close()
print("data written")
print("processing done")
def check_ping():
hostname = "google.com"
response = os.system("ping -c 1 " + hostname)
return response
if check_ping() == 0:
# ping success, record it now
record(True)
else:
# Could not ping, record it now
record(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment