Last active
September 25, 2015 23:18
-
-
Save rvause/1000745 to your computer and use it in GitHub Desktop.
Simple script to watch my internet connection when I am having trouble with my ISP. Checks every minute and logs a note if there's a problem
This file contains 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
#!/usr/bin/env python | |
import time | |
import subprocess | |
from datetime import datetime | |
LOG_FILE = 'connection.log' | |
def check(ip): | |
try: | |
result = subprocess.check_call( | |
['ping', '-c', '4', '-w', '10', ip], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE | |
) | |
except subprocess.CalledProcessError, e: | |
result = e.returncode | |
if result > 0: | |
f = open(LOG_FILE, 'a') | |
f.write('%s Failed[%s] %s\n' % ( | |
datetime.strftime(datetime.now(), '%d/%m/%y %X'), | |
result, | |
ip | |
) | |
) | |
f.close() | |
def watch_connection(): | |
f = open(LOG_FILE, 'w') | |
f.write('Begin at %s\n' % datetime.strftime(datetime.now(), '%d/%m/%y %X')) | |
f.close() | |
while True: | |
check('192.168.1.254') # Router | |
check('8.8.8.8') # Somewhere else | |
time.sleep(60) | |
if __name__ == '__main__': | |
watch_connection() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment