zonhub/monit.py

106 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import hashlib
import re
from datetime import datetime
import requests
from bs4 import BeautifulSoup
#from tabulate import tabulate
import mysql.connector
payload = {
'active_page': 'page_login',
'prev_page': '',
'page_title': 'Login',
'intercept_id': '-2',
'no_dns': '0',
'mimic_button_field': 'submit_button_login_submit%3A+..',
'button_value': '',
'strip_page_top': '0',
'scroll_top': '0',
'post_id': '0',
'page_title_text': 'Login',
'page_icon_number': '30',
'defval_lang': '1',
'defval_username': '',
'md5_pass': '53c8a95730e66905d86b03ae7eec26b0',
'auth_key': '1216289424',
'lang=': '1',
'username': 'home',
#'password_1636230399':''
}
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)
#ts = time.time()
ts = datetime.now()
r = requests.get('http://192.168.1.1/index.cgi?active_page=page_equipment_status', cookies=cookies)
soup = BeautifulSoup(r.text, 'html.parser')
tables = soup.find_all('table')
downstream = tables[60]
upstream = tables[61]
#print(" --- Canais Downstream ---")
downtable = []
keys = ['rx_id_val_dn_', 'status_val_dn_', 'freq_val_dn_', 'power_val_dn_', 'snr_val_dn_']
for x in range(0, 8):
row = []
for key in keys:
row.append(downstream.find("td", {"id": key + str(x)}).renderContents().strip()
.decode('utf-8').replace(' MHz', '').replace(' dBmV', '').replace(' dB', '')
)
downtable.append(row)
#headers = ['ID Recetor', 'Estado', 'Frequência (MHz)', 'Potência (dBmV)', 'SNR (dB)']
#print(tabulate(downtable, headers=headers))
#print("\n --- Canais Upstream ---")
uptable = []
keys = ['rx_id_val_dn_', 'cannel_id_val_dn_', 'freq_val_dn_', 'power_val_dn_']
for x in range(0, 2):
row = []
for key in keys:
row.append(upstream.find("td", {"id": key + str(x)}).renderContents().strip()
.decode('utf-8').replace(' MHz', '').replace(' dBmV', '')
)
uptable.append(row)
#headers = ['ID Recetor', 'Channel ID', 'Frequência (MHz)', 'Potência (dBmV)']
#print(tabulate(uptable, headers=headers, tablefmt="psql"))
cnx = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='monit')
cursor = cnx.cursor()
add_downstream = ("INSERT INTO downstream "
"(ts, id_recetor, estado, frequencia, potencia, snr) "
"VALUES (%s, %s, %s, %s, %s, %s)")
add_upstream = ("INSERT INTO upstream "
"(ts, id_recetor, channel_id, frequencia, potencia) "
"VALUES (%s, %s, %s, %s, %s)")
for line in downtable:
cursor.execute(add_downstream, (ts, line[0], line[1], line[2], line[3], line[4]))
for line in uptable:
cursor.execute(add_upstream, (ts, line[0], line[1], line[2], line[3]))
cnx.commit()
cursor.close()
cnx.close()