Skip to content

Instantly share code, notes, and snippets.

@scottharman
Created January 30, 2017 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottharman/6ca07a7c46ca09de3e3b2f0a5094d86e to your computer and use it in GitHub Desktop.
Save scottharman/6ca07a7c46ca09de3e3b2f0a5094d86e to your computer and use it in GitHub Desktop.
Python Simple Router Stats
import sys
import re
from time import sleep
import requests
from requests.auth import HTTPBasicAuth
from bs4 import BeautifulSoup
from termcolor import colored
import json
import logging
import logging.config
import loggly.handlers
logging.config.fileConfig('loggly.conf')
logger = logging.getLogger('myLogger')
old_fields = {}
#logger.info('Test log')
STATUS_PAGE_URL = "http://192.168.1.1/RST_statistic.htm"
CREDS = ('admin', 'password')
#STATUS_PAGE_TEST = open("router_stats.html")
while True:
try:
stats = BeautifulSoup(requests.get(STATUS_PAGE_URL, auth=HTTPBasicAuth(*CREDS)).text, "html.parser")
#stats = BeautifulSoup(open("router_stats.html"), "html.parser")
except requests.exceptions.ConnectionError:
print colored("Unable to connect", 'red')
sys.exit()
script = stats.findAll('script')[1]
pattern = re.compile('(\w+)="(.*?)Mbps|dB"')
fields = dict(re.findall(pattern, script.text))
clean_fields = { k:v.strip() for k, v in fields.iteritems()}
if old_fields != clean_fields:
logger.info(json.dumps(clean_fields))
old_fields = clean_fields
print clean_fields
sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment