Skip to content

Instantly share code, notes, and snippets.

@jalexandre0
Last active December 16, 2015 20:00
Show Gist options
  • Save jalexandre0/5489553 to your computer and use it in GitHub Desktop.
Save jalexandre0/5489553 to your computer and use it in GitHub Desktop.
###WIP### Check internet and change default routes if three sites are down.
#!/usr/bin/env python
import urllib2
import subprocess
import commands
debug = 1
def open_url(url):
try:
response=urllib2.urlopen(url,timeout=5)
return True
except urllib2.URLError as err:
return False
def parse_route():
output = commands.getstatusoutput('ip -o route get 4.2.2.2 from default')
output = output[1].split(" ")
return output[2]
if debug == 0:
url_1 = open_url("http://www.google.com.br")
url_2 = open_url("http://www.uol.com.br")
url_3 = open_url("http://www.amazon.com")
elif debug == 1:
url_1 = open_url("http://thissiteshouldfail.com/")
url_2 = open_url("http://thissiteshouldfail.com/")
url_3 = open_url("http://thissiteshouldfail.com/")
else:
url_1 = open_url("http://www.google.com.br")
url_2 = open_url("http://www.uol.com.br")
url_3 = open_url("http://thissiteshouldfail.com")
if url_1 or url_2 or url_3:
print "Must return ok to monitoring software"
else:
#Need finish this in a nice and beautiful way ;)
route = parse_route()
route_del = "ip route del %s " %(route )
route_add = "ip route add 192.168.0.9 default"
print "commands.getstatusoutput(route_del)"
print "commands.getstatusoutput(route_add)"
print "Must return change to monitoring software"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment