2013-04-08 10:05:06 +00:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2013-04-08 18:18:08 +00:00
|
|
|
import re
|
|
|
|
|
2013-04-08 11:04:44 +00:00
|
|
|
import config
|
2013-04-08 18:18:08 +00:00
|
|
|
import lcd
|
2013-04-08 10:05:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _get_process_list():
|
|
|
|
pids = [int(pid) for pid in os.listdir('/proc') if pid.isdigit()]
|
|
|
|
processes = []
|
|
|
|
for pid in pids:
|
2013-04-25 12:34:29 +00:00
|
|
|
try:
|
2013-04-25 12:40:44 +00:00
|
|
|
with open("/proc/%i/cmdline" % pid, "r") as f:
|
|
|
|
processes.append(f.read())
|
2013-04-25 12:34:29 +00:00
|
|
|
except:
|
|
|
|
pass
|
2013-04-08 10:05:06 +00:00
|
|
|
return zip(pids, processes)
|
|
|
|
|
|
|
|
|
|
|
|
def _start_pppd():
|
2013-04-08 13:47:36 +00:00
|
|
|
print " ".join(config.PPPD)
|
2013-04-08 11:04:44 +00:00
|
|
|
p = subprocess.Popen(config.PPPD, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2013-04-08 10:05:06 +00:00
|
|
|
stdout, stderr = p.communicate()
|
|
|
|
if p.returncode != 0:
|
|
|
|
sys.stderr.write("Could not run pppd succesfully! Here is some debug data:\n")
|
|
|
|
sys.stderr.write(" STDOUT: %s\n" % stdout)
|
|
|
|
sys.stderr.write(" STDERR: %s\n" % stderr)
|
|
|
|
else:
|
|
|
|
sys.stdout.write("PPPd started succesfully.\n")
|
|
|
|
|
|
|
|
|
|
|
|
def _get_pppd_pid():
|
2013-04-08 13:47:36 +00:00
|
|
|
for pid, process in _get_process_list():
|
2013-04-08 18:21:29 +00:00
|
|
|
if process.startswith("/usr/sbin/pppd"):
|
2013-04-08 10:05:06 +00:00
|
|
|
return pid
|
|
|
|
|
|
|
|
|
|
|
|
def _kill_pppd():
|
|
|
|
pid = _get_pppd_pid()
|
|
|
|
if pid:
|
|
|
|
os.kill(pid, 9)
|
|
|
|
sys.stdout.write("Killed PPPd.\n")
|
|
|
|
|
|
|
|
|
|
|
|
def _check_connectivity():
|
2013-04-08 11:04:44 +00:00
|
|
|
p = subprocess.Popen(config.PING, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2013-04-08 10:05:06 +00:00
|
|
|
p.communicate()
|
|
|
|
if p.returncode != 0:
|
|
|
|
sys.stderr.write("There seem to be some connectivity problems... Waiting 15 seconds, then giving it another shot...\n")
|
|
|
|
time.sleep(15)
|
|
|
|
|
2013-04-08 11:04:44 +00:00
|
|
|
p = subprocess.Popen(config.PING, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2013-04-08 10:05:06 +00:00
|
|
|
p.communicate()
|
|
|
|
if p.returncode != 0:
|
|
|
|
sys.stderr.write("Connectivity problems persisting! :(\n")
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
sys.stderr.write("Seems to be okay now.\n")
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2013-04-08 18:18:08 +00:00
|
|
|
def _get_public_ip():
|
|
|
|
p = subprocess.Popen(["ip", "a", "list", "dev", "ppp0"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
stderr, stdout = p.communicate()
|
|
|
|
if p.returncode != 0:
|
|
|
|
return "UNKNOWN"
|
|
|
|
else:
|
|
|
|
for line in stderr.split("\n"):
|
|
|
|
m = re.match("^inet ([\.0-9]*) peer", line.strip())
|
|
|
|
if m:
|
|
|
|
return m.group(1)
|
|
|
|
return "UNKNOWN"
|
|
|
|
|
|
|
|
|
2013-04-08 10:05:06 +00:00
|
|
|
def loop():
|
2013-04-08 18:18:08 +00:00
|
|
|
screen = lcd.LCDScreen()
|
|
|
|
|
|
|
|
def log(text, text2=None, clear=True):
|
|
|
|
if clear:
|
|
|
|
screen.clear()
|
|
|
|
text = text[:16]
|
|
|
|
text = ' ' * ((16 - len(text))/2) + text
|
|
|
|
screen.text_at(0, 0, text)
|
|
|
|
if text2:
|
|
|
|
text2 = text2[:16]
|
|
|
|
text2 = ' ' * ((16 - len(text2))/2) + text2
|
|
|
|
screen.text_at(0, 1, text2)
|
|
|
|
|
2013-04-08 10:05:06 +00:00
|
|
|
while True:
|
|
|
|
pppd_pid = _get_pppd_pid()
|
|
|
|
if not pppd_pid:
|
2013-04-08 18:18:08 +00:00
|
|
|
log("Starting PPPd...")
|
|
|
|
_start_pppd()
|
|
|
|
screen.clear()
|
|
|
|
|
|
|
|
timeout = 30
|
|
|
|
while timeout >= 0:
|
|
|
|
log("PPPd started.", "Testing in %i s. " % timeout, False)
|
|
|
|
time.sleep(1)
|
|
|
|
timeout -= 1
|
|
|
|
|
|
|
|
log("PPPd started.", "Testing...")
|
|
|
|
|
|
|
|
if not _check_connectivity():
|
|
|
|
sys.stderr.write("Restarting PPPd.\n")
|
|
|
|
log("ADSL down!", "Restarting PPPd...")
|
|
|
|
_kill_pppd()
|
2013-04-08 10:05:06 +00:00
|
|
|
_start_pppd()
|
2013-04-08 18:18:08 +00:00
|
|
|
log("ADSL down!", "PPPd restarted!")
|
2013-04-08 10:05:06 +00:00
|
|
|
else:
|
2013-04-08 18:18:08 +00:00
|
|
|
log("ADSL up!", _get_public_ip())
|
|
|
|
time.sleep(60)
|