94 lines
2.9 KiB
Python
Executable File
94 lines
2.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import hashlib
|
|
import re
|
|
from datetime import datetime
|
|
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
|
|
|
|
matches = re.search(r"<INPUT type=HIDDEN name=\"auth_key\" value=\"(.+?)\">", r.text)
|
|
auth_key = matches[1]
|
|
m = hashlib.md5()
|
|
m.update(('%s%s' % ('zonnet', auth_key)).encode('utf-8'))
|
|
hexdigest = m.hexdigest()
|
|
|
|
payload['auth_key'] = auth_key
|
|
payload['md5_pass'] = hexdigest
|
|
|
|
r = requests.post('http://192.168.1.1/index.cgi', data=payload, cookies=cookies)
|
|
|
|
payload = {
|
|
'cable_settings': 1,
|
|
'active_page': 'page_cable_settings',
|
|
}
|
|
|
|
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)")
|
|
|
|
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)
|
|
|
|
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")
|
|
|
|
cnx = mysql.connector.connect(user='root', password='',
|
|
host='127.0.0.1',
|
|
database='monit')
|
|
cursor = cnx.cursor()
|
|
|
|
print(uts, "Connected to DB")
|
|
|
|
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
|
|
|
|
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
|
|
|
|
cursor.close()
|
|
cnx.commit()
|
|
cnx.close()
|
|
print(uts, "DB connection closed")
|
|
print(uts, "Sleeping")
|
|
time.sleep(30) |