Created
September 1, 2020 16:15
-
-
Save yutsuku/27875897b4214710942343ccae5b87c9 to your computer and use it in GitHub Desktop.
Netgear R6220 Router Traffic meter scrapper
This file contains hidden or 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
# | |
# Netgear R6220 Router Traffic meter scrapper | |
# | |
# This script will login into the router webpage in order to obtain last month | |
# download/upload statistics and save it as CSV file. | |
# See --help for details | |
# | |
# The script assumes that you have: | |
# - enabled traffic meter, | |
# - set reset counter to 1st day of each month | |
# | |
# Author: [email protected] | |
# Revision: 2020-09-01 | |
# Dependencies: requests | |
# License: MIT License | |
# | |
import sys | |
import os | |
import requests | |
import re | |
import json | |
import datetime | |
import csv | |
from requests.auth import HTTPBasicAuth | |
from pathlib import Path | |
from argparse import ArgumentParser | |
parser = ArgumentParser() | |
parser.add_argument("-o", "--output", help="Where to save CSV report file", default="./router_traffic_report.csv") | |
parser.add_argument("-u", "--user", help="Username", default="admin") | |
parser.add_argument("-p", "--password", help="Password", default="password") | |
parser.add_argument("-e", "--endpoint", help="Page containing traffic data", default="http://192.168.1.1/traffic_meter.htm&todo=cfg_init") | |
args = parser.parse_args() | |
username = args.user | |
password = args.password | |
report_filename = args.output | |
traffic_endpoint = args.endpoint | |
def lazy_search(text): | |
for line in text.splitlines(): | |
if line.startswith('var report=[['): | |
p = re.compile('(?<=report=)(.*)(?=;)') | |
parsed = p.findall(line) | |
newParsed = json.loads(parsed[0]) | |
download = newParsed[4][1].split('/')[0] or None | |
upload = newParsed[4][2].split('/')[0] or None | |
if download and upload: | |
return download, upload | |
return None | |
def write_report(filename, time_period, download, upload): | |
# write header | |
my_file = Path(filename) | |
if not my_file.is_file(): | |
os.makedirs(os.path.dirname(filename), exist_ok=True) | |
with open(filename, encoding='utf8', mode='w', newline='') as file: | |
writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) | |
writer.writerow(["Period", "Download (Mbytes)", "Upload (Mbytes)"]) | |
# make sure we don't write duplicate | |
with open(filename, encoding='utf8', mode='r', newline='') as file: | |
reader = csv.reader(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) | |
for row in reader: | |
if row[0] == time_period: | |
return | |
# write new data | |
with open(filename, encoding='utf8', mode='a', newline='') as file: | |
writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) | |
writer.writerow([time_period, download, upload]) | |
session = requests.Session() | |
session.auth = (username, password) | |
auth = session.get(traffic_endpoint) | |
response = session.get(traffic_endpoint) | |
if response.status_code != 200: | |
sys.exit('Cannot log in') | |
download, upload = lazy_search(response.text) | |
if not download: | |
sys.exit('Unable to obtain data') | |
# get date of previous month for the report | |
today = datetime.date.today() | |
first = today.replace(day=1) | |
lastMonth = first - datetime.timedelta(days=1) | |
print("Writing to", report_filename) | |
write_report(report_filename, lastMonth.strftime("%Y-%m-%d"), download, upload) | |
print('Done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment