Last active
August 29, 2015 13:56
-
-
Save puehcl/9252691 to your computer and use it in GitHub Desktop.
login/parse script for the akademikerhilfe traffic control system, a data.txt file with the ip of the gateway, the username and the password (each in its own line) must be supplied in the same folder
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/lib/python2.7 | |
import re | |
import sys | |
import urllib | |
import urllib2 | |
import datetime | |
LOGIN_DATA_FILE = "data.txt" | |
PAGE_LOGOUT = "logout.php" | |
PAGE_LOGIN = "login.php" | |
PAGE_STATS = "index.php" | |
ARG_LOGOUT = "logout" | |
ARG_LOGIN = "login" | |
ARG_RELOG = "relog" | |
ARG_PARSE_TRAFFIC = "parse" | |
ARG_WAIT_FOR_CONNECTION = "wait" | |
class LogoutPage(object): | |
URI = "logout.php" | |
class LoginPage(object): | |
URI = "login.php" | |
FORM_NAME_USERNAME = "username" | |
FORM_NAME_PASSWORD = "password" | |
class StatsPage(object): | |
URI = "index.php" | |
class Infolog(object): | |
logfile = open("infolog.txt", "a") | |
@staticmethod | |
def log(text): | |
date = datetime.datetime.now() | |
s = "%s: %s" % (str(date), text) | |
Infolog.logfile.write(s + "\n") | |
print s | |
class Datalog(object): | |
logfile = open("datalog.csv", "a") | |
@staticmethod | |
def log(ctu, ctd, at, rt, ttl): | |
date = datetime.datetime.now() | |
s = "%s, %d, %d, %d, %d, %s" % (str(date), ctu, ctd, at, rt, ttl) | |
Datalog.logfile.write(s + "\n") | |
print s | |
class PostForm(object): | |
def __init__(self, values={}, action=None): | |
self.values = values | |
self.action = action | |
def set_value(self, key, value): | |
self.values[key] = value | |
def set_action(self, action): | |
self.action = action | |
def post(self, timeout=30): | |
data = urllib.urlencode(self.values) | |
req = urllib2.Request(self.action, data) | |
try: | |
resp = urllib2.urlopen(req, timeout=timeout) | |
return resp.read() | |
except urllib2.URLError as e: | |
Infolog.log("could not open %s: %s" % (self.action, e.reason)) | |
return None | |
class TrafficHelper(object): | |
ctu_regex = re.compile("Upload: [0-9]+(\.[0-9]+)?") | |
ctd_regex = re.compile("Download: [0-9]+(\.[0-9]+)?") | |
at_regex = re.compile("Sie haben [0-9]+(\.[0-9]+)?") | |
rt_regex = re.compile("Traffic: [0-9]+(\.[0-9]+)?") | |
ttl_regex = re.compile("[0-3][0-9]\.[0-1][0-9]\.[0-9]{4} [0-2][0-9]:[0-5][0-9]") | |
def __init__(self, html): | |
self.ctu = None | |
self.ctd = None | |
self.at = None | |
self.rt = None | |
self.ttl = None | |
match = TrafficHelper.ctu_regex.search(html) | |
if match: | |
self.ctu = float(match.group().split(" ")[1]) | |
match = TrafficHelper.ctd_regex.search(html) | |
if match: | |
self.ctd = float(match.group().split(" ")[1]) | |
match = TrafficHelper.at_regex.search(html) | |
if match: | |
self.at = float(match.group().split(" ")[2]) | |
match = TrafficHelper.rt_regex.search(html) | |
if match: | |
self.rt = float(match.group().split(" ")[1]) | |
match = TrafficHelper.ttl_regex.search(html) | |
if match: | |
dt = match.group().split(" ") | |
date = [int(d) for d in dt[0].split(".")] | |
time = [int(t) for t in dt[1].split(":")] | |
self.ttl = datetime.datetime(date[2], date[1], date[0], time[0], time[1]) | |
def get_commercial_traffic_up(self): | |
return self.ctu | |
def get_commercial_traffic_down(self): | |
return self.ctd | |
def get_academic_traffic(self): | |
return self.at | |
def get_remaining_traffic(self): | |
return self.rt | |
def get_time_when_logout(self): | |
return self.ttl | |
def get_login_data(fil): | |
login = open(fil, "r") | |
ip = login.readline()[:-1] | |
name = login.readline()[:-1] | |
pwd = login.readline()[:-1] | |
return ip, name, pwd | |
def get_url(ip, uri): | |
return "http://%s/%s" % (ip, uri) | |
def wait_for_connection(ip): | |
Infolog.log("waiting for active connection to host at %s" % ip) | |
while True: | |
try: | |
urllib2.urlopen(get_url(ip, ""), timeout=5) | |
break | |
except urllib2.URLError as e: | |
pass | |
Infolog.log("connection to host at %s active" % ip) | |
def logout(ip): | |
form = PostForm() | |
form.set_action(get_url(ip, LogoutPage.URI)) | |
if form.post(): | |
Infolog.log("successfully logged out") | |
def login(ip, username, password): | |
form = PostForm() | |
form.set_value(LoginPage.FORM_NAME_USERNAME, username) | |
form.set_value(LoginPage.FORM_NAME_PASSWORD, password) | |
form.set_action(get_url(ip, LoginPage.URI)) | |
if form.post(): | |
Infolog.log("successfully logged in as %s" % username) | |
def relog(ip, username, password): | |
logout(ip) | |
login(ip, username, password) | |
def parse(ip): | |
html = PostForm(action=get_url(ip, StatsPage.URI)).post() | |
if not html: | |
return | |
th = TrafficHelper(html) | |
ctd = th.get_commercial_traffic_down() | |
ctu = th.get_commercial_traffic_up() | |
at = th.get_academic_traffic() | |
rt = th.get_remaining_traffic() | |
ttl = th.get_time_when_logout() | |
if (not ctd or not ctu or not at or not rt or not ttl): | |
Infolog.log("could not get stats, not logged in or wrong regex configuration") | |
Infolog.log("ctd:" + str(ctd) + ",ctu:" + str(ctu) + ",at:" + str(at) + ",rt:" + str(rt) + ",ttl:" + str(ttl)) | |
return | |
Datalog.log(ctd, ctu, at, rt, ttl) | |
def usage(): | |
Infolog.log("no arguments supplied") | |
print "usage: %s arg1 arg2 ... argn" % sys.argv[0] | |
print "args (order matters):" | |
print "---------------------" | |
print "wait\twaits until a connection to the host at the specified ip can be established (useful at boot)" | |
print "login\tlogs the user in with the credentials specified in data.txt" | |
print "logout\tlogs the user out of the system" | |
print "relog\tperforms a logout and a subsequent login" | |
print "parse\tparses traffic data from the stats page and prints the data as csv into datalog.csv" | |
print "---------------------" | |
if __name__ == "__main__": | |
if len(sys.argv) == 1: | |
usage() | |
args = sys.argv[1:] | |
ip, username, password = get_login_data(LOGIN_DATA_FILE) | |
for arg in args: | |
if arg == ARG_WAIT_FOR_CONNECTION: | |
wait_for_connection(ip) | |
if arg == ARG_LOGOUT: | |
logout(ip) | |
if arg == ARG_LOGIN: | |
login(ip, username, password) | |
if arg == ARG_RELOG: | |
relog(ip, username, password) | |
if arg == ARG_PARSE_TRAFFIC: | |
parse(ip) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment