Created
January 30, 2017 02:22
-
-
Save scottharman/6ca07a7c46ca09de3e3b2f0a5094d86e to your computer and use it in GitHub Desktop.
Python Simple Router Stats
This file contains 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
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