adsl-bouncer/adsl.py

71 lines
1.9 KiB
Python

import os
import time
import sys
import subprocess
import config
def _get_process_list():
pids = [int(pid) for pid in os.listdir('/proc') if pid.isdigit()]
processes = []
for pid in pids:
f = open("/proc/%i/cmdline" % pid, "r")
processes.append(f.read())
f.close()
return zip(pids, processes)
def _start_pppd():
p = subprocess.Popen(config.PPPD, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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():
for process, pid in _get_process_list():
if process.startswith("pppd"):
return pid
def _kill_pppd():
pid = _get_pppd_pid()
if pid:
os.kill(pid, 9)
sys.stdout.write("Killed PPPd.\n")
def _check_connectivity():
p = subprocess.Popen(config.PING, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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)
p = subprocess.Popen(config.PING, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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
def loop():
while True:
pppd_pid = _get_pppd_pid()
if not pppd_pid:
_start_pppd()
time.sleep(60)
else:
if not _check_connectivity():
sys.stderr.write("Restarting PPPd.\n")
_kill_pppd()
_start_pppd()
time.sleep(60)