From e1d87143c92a669442830bee8ce48b601da7a462 Mon Sep 17 00:00:00 2001 From: Pedro de Oliveira Date: Mon, 23 Oct 2017 03:32:13 +0100 Subject: [PATCH] Update --- ajaxmonit.py | 172 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 103 insertions(+), 69 deletions(-) diff --git a/ajaxmonit.py b/ajaxmonit.py index 956b5f9..71422a7 100755 --- a/ajaxmonit.py +++ b/ajaxmonit.py @@ -1,94 +1,128 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +""" +Logs the signal quality levels from the Hitron CVE30360 Cable modem to MySQL +""" + import hashlib import re -from datetime import datetime +from datetime import datetime, timedelta import time import requests import mysql.connector -payload = { - 'active_page': 'page_login', - 'md5_pass': '', - 'auth_key': '', - 'username': 'home', -} -r = requests.get('http://192.168.1.1/') -cookies = r.cookies +def login(): + """ + Logins into the webserver, returns a cookie to be used in the requests + """ + payload = { + 'active_page': 'page_login', + 'md5_pass': '', + 'auth_key': '', + 'username': 'home', + } -matches = re.search(r"", r.text) -auth_key = matches[1] -m = hashlib.md5() -m.update(('%s%s' % ('zonnet', auth_key)).encode('utf-8')) -hexdigest = m.hexdigest() + req = requests.get('http://192.168.1.1/') + cookies = req.cookies -payload['auth_key'] = auth_key -payload['md5_pass'] = hexdigest + matches = re.search(r'', + req.text) + auth_key = matches[1] + md5 = hashlib.md5() + md5.update(('%s%s' % ('zonnet', auth_key)).encode('utf-8')) + hexdigest = md5.hexdigest() -r = requests.post('http://192.168.1.1/index.cgi', data=payload, cookies=cookies) + payload['auth_key'] = auth_key + payload['md5_pass'] = hexdigest -payload = { - 'cable_settings': 1, - 'active_page': 'page_cable_settings', -} + req = requests.post('http://192.168.1.1/index.cgi', data=payload, + cookies=cookies) + return cookies -fields = { - 'download': ['snr_val_dn', 'power_val_dn', 'freq_val_dn'], - 'upload': ['power_val_up', 'freq_val_up', 'cannel_id_val_up'], - } -add_ajax = ("INSERT INTO ajax " - "(ts, direction, channel, frequency, power, snr) " - "VALUES (%s, %s, %s, %s, %s, %s)") +def dbconnect(): + """ + Returns a MySQL connection + """ + return mysql.connector.connect(user='root', password='', + host='127.0.0.1', database='monit') -while True: - ts = datetime.now() - r = requests.post('http://192.168.1.1/index.cgi?rand=' + str(int(ts.strftime("%s"))), data=payload, cookies=cookies) +def page_cable_settings(cookies): + payload = { + 'cable_settings': 1, + 'active_page': 'page_cable_settings', + } + ts = datetime.now() + r = requests.post('http://192.168.1.1/index.cgi?rand=' + + str(int(ts.strftime("%s"))), data=payload, + cookies=cookies) + return r - rts = datetime.strptime(r.headers['Date'], '%a, %d %b %Y %H:%M:%S GMT') - uts = rts.strftime('%Y-%m-%d %H:%M:%S') - print(uts, "Got page") +if __name__ == "__main__": + FIELDS = { + 'download': ['snr_val_dn', 'power_val_dn', 'freq_val_dn'], + 'upload': ['power_val_up', 'freq_val_up', 'cannel_id_val_up'], + } - cnx = mysql.connector.connect(user='root', password='', - host='127.0.0.1', - database='monit') - cursor = cnx.cursor() + INSERT = ("INSERT INTO ajax " + "(ts, direction, channel, frequency, power, snr) " + "VALUES (%s, %s, %s, %s, %s, %s)") - print(uts, "Connected to DB") + REGEX = "change_field_if_exist\('(%s)_(%s)', '(.+?)'\);" - for x in range(0, 8): - channel = {} - for f in fields['download']: - matches = re.search(r"change_field_if_exist\('(" + f + ")_(" + str(x) + ")', '(.+?)'\);", r.text) - if matches: - channel[f] = matches.group(3).replace(' dBmV', '').replace(' dB', '').replace(' MHz', '') - try: - #print(uts, 0, x, channel['freq_val_dn'], channel['power_val_dn'], channel['snr_val_dn']) - print(uts, "Inserting downstream to DB") - cursor.execute(add_ajax, (uts, 0, x, channel['freq_val_dn'], channel['power_val_dn'], channel['snr_val_dn'])) - except (mysql.connector.IntegrityError, KeyError) as err: - pass + COOKIES = login() + while True: + print(datetime.now(), "Request page") + REQ = page_cable_settings(COOKIES) + print(datetime.now(), "Got page") + UTS = (datetime.strptime(REQ.headers['Date'], + '%a, %d %b %Y %H:%M:%S %Z') + timedelta(hours=1) + ).strftime('%Y-%m-%d %H:%M:%S') - for x in range(0, 2): - channel = {} - for f in fields['upload']: - matches = re.search(r"change_field_if_exist\('(" + f + ")_(" + str(x) + ")', '(.+?)'\);", r.text) - if matches: - channel[f] = matches.group(3).replace(' dBmV', '').replace(' MHz', '') - try: - #print(uts, 1, channel['cannel_id_val_up'], channel['freq_val_up'], channel['power_val_up']) - print(uts, "Inserting upstream to DB") - cursor.execute(add_ajax, (uts, 1, channel['cannel_id_val_up'], channel['freq_val_up'], channel['power_val_up'], None)) - except (mysql.connector.IntegrityError, KeyError) as err: - pass + CNX = dbconnect() + CURSOR = CNX.cursor() + print(datetime.now(), "Connected to DB") - cursor.close() - cnx.commit() - cnx.close() - print(uts, "DB connection closed") - print(uts, "Sleeping") - time.sleep(30) \ No newline at end of file + for CHAN in range(0, 8): + CHANNEL = {} + for FIELD in FIELDS['download']: + + MATCHES = re.search(REGEX % (FIELD, str(CHAN)), REQ.text) + if MATCHES: + CHANNEL[FIELD] = MATCHES.group(3).replace(' dBmV', '').replace(' dB', '').replace(' MHz', '') + try: + CURSOR.execute(INSERT, (UTS, 0, CHAN, + CHANNEL['freq_val_dn'], + CHANNEL['power_val_dn'], + CHANNEL['snr_val_dn'])) + print(datetime.now(), + "Inserting downstream channel %d to DB" % (CHAN)) + except (mysql.connector.IntegrityError, KeyError) as err: + pass + + for CHAN in range(0, 2): + CHANNEL = {} + for FIELD in FIELDS['upload']: + MATCHES = re.search(REGEX % (FIELD, str(CHAN)), REQ.text) + if MATCHES: + CHANNEL[FIELD] = MATCHES.group(3).replace(' dBmV', '').replace(' MHz', '') + try: + CURSOR.execute(INSERT, (UTS, 1, + CHANNEL['cannel_id_val_up'], + CHANNEL['freq_val_up'], + CHANNEL['power_val_up'], None)) + print(datetime.now(), + "Inserting upstream channel %d to DB" % (CHAN)) + except (mysql.connector.IntegrityError, KeyError) as err: + pass + + CURSOR.close() + CNX.commit() + CNX.close() + print(datetime.now(), "DB connection closed") + print(datetime.now(), "Sleeping") + time.sleep(30)