Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robbiet480/1252a3f6258f751604df94da68a92ea6 to your computer and use it in GitHub Desktop.
Save robbiet480/1252a3f6258f751604df94da68a92ea6 to your computer and use it in GitHub Desktop.
This script is meant to be run with the Motorola SB6121 Surfboard Modem.
#!/usr/bin/env python
"""
Sample a Motorola Surfboard modem's signal.
This script is meant to be run with the Motorola SB6121 Surfboard Modem.
If you run it on a different modem, it might not work, but it's worth a try...
It should be run as a cron job, every minute (or whatever interval you'd like)
into a text file. The file can later be used by a json parser to analyze the
signal over time, which can be used to come to conclusions that you can argue
with the cable company's tech support about when the service goes down.
"""
import sys
from optparse import OptionParser
import logging
from datetime import datetime
import json
import requests
from bs4 import BeautifulSoup
# Default paramaters to be used by option parser
DEFAULT_SIGNAL_URL = "http://192.168.100.1/cmSignalData.htm"
# Set up logging
_LOGGER = logging.getLogger(__name__)
_STREAM_HANDLER = logging.StreamHandler()
_LOG_FORMATTER = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
_STREAM_HANDLER.setFormatter(_LOG_FORMATTER)
_LOGGER.addHandler(_STREAM_HANDLER)
def set_log_level(verbosity):
"""Set the log level based on an integer between 0 and 2."""
log_level = logging.WARNING # default
if verbosity:
if verbosity == 1:
log_level = logging.INFO
elif verbosity >= 2:
log_level = logging.DEBUG
_LOGGER.setLevel(level=log_level)
def get_options():
"""
Return the options and arguments passed to this script on the command line.
@return: (options,args)
"""
usage = "usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option("-u", "--signal-url", dest="signal_url",
default=DEFAULT_SIGNAL_URL,
help="The signal url default: [default: %default]")
parser.add_option('-v', '--verbose', dest='verbose', action='count',
help="Increase verbosity (can specify multiple times)")
options, args = parser.parse_args()
return (options, args)
def main(args):
"""Get the modem signal stats."""
(options, args) = get_options()
set_log_level(options.verbose)
_LOGGER.debug("Starting with options: %s", options)
timestamp = datetime.now().isoformat()
try:
request = requests.get(options.signal_url)
soup = BeautifulSoup(request.content, 'html.parser')
except BaseException:
print(json.dumps({'timestamp': timestamp, 'res': "error"}))
return
res = {}
for table in soup.find_all("table"):
if not table.th:
continue
heading = table.th.font.contents[0].strip()
res[heading] = {}
if heading:
for table_row in table.find_all("tr"):
if table_row.th:
continue
row_data = []
for strs in table_row.stripped_strings:
strip_str = "The Downstream Power Level reading is a"
if strs.startswith(strip_str):
continue
row_data.append(strs)
if row_data:
res[heading][row_data[0]] = row_data[1:]
print(json.dumps({'timestamp': timestamp, 'res': res}))
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment