Last active
April 11, 2018 23:30
-
-
Save juanvmarquezl/8aa008b441738c89fdb6cc2407a9a735 to your computer and use it in GitHub Desktop.
Test internet connection, if fail reboot then router
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
# -*- encoding: utf-8 -*- | |
# Test internet connection, if fail reboot router | |
import urllib2 | |
import time | |
import base64 | |
def internet_on(): | |
try: | |
urllib2.urlopen('http://www.google.com', timeout=20) | |
return True | |
except urllib2.URLError as err: | |
print err | |
pass | |
return False | |
def reboot_router(): | |
''' | |
https://stackoverflow.com/questions/10195915/python-urllib2- | |
httperror-http-error-401-unauthorized | |
http://www.jfwhome.com/2012/06/18/reboot-tp-link- | |
router-remotely-or-automatically/ | |
wget -qO- --user=username --password=password http://192.168.1.1/userRpm/ | |
SysRebootRpm.htm?Reboot=Reboot > /dev/null | |
''' | |
router_ip = u'192.168.1.1' | |
url = u'http://' + router_ip + u'/userRpm/SysRebootRpm.htm?Reboot=Reboot' | |
username = u'admin' | |
password = u'admin' | |
p = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
p.add_password(None, url, username, password) | |
handler = urllib2.HTTPBasicAuthHandler(p) | |
opener = urllib2.build_opener(handler) | |
urllib2.install_opener(opener) | |
urllib2.urlopen(url).read() | |
def reboot_router2(): | |
ip = u'192.168.1.1' | |
login_user = 'admin' | |
login_pw = 'admin' | |
url = 'http://' + ip + \ | |
'/userRpm/SysRebootRpm.htm?Reboot=%D6%D8%C6%F4%C2%B7%D3%C9%C6%F7' | |
auth = 'Basic ' + base64.b64encode(login_user + ':' + login_pw) | |
print auth | |
heads = {'Referer': 'http://' + ip + '/userRpm/SysRebootRpm.htm', | |
'Authorization': auth | |
} | |
request = urllib2.Request(url, None, heads) | |
response = urllib2.urlopen(request) | |
print response.read() | |
att = time.strftime('%Y-%m-%d %H:%M:%S') | |
interval = 120 # Seconds | |
print '%s | Starting internet monitor...' % att | |
print ' | Interval set to %s seconds' % interval | |
while True: | |
att = time.strftime('%Y-%m-%d %H:%M:%S') | |
if not internet_on(): | |
print '%s | Conection lost, rebooting router...' % att, | |
reboot_router2() | |
print 'Done.' | |
time.sleep(interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment