Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeffp123/2562c1ec1b6958250521 to your computer and use it in GitHub Desktop.
Save jeffp123/2562c1ec1b6958250521 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 the 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
import os
from optparse import OptionParser
import fileinput
import logging
from collections import namedtuple
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import json
# Default paramaters to be used by option parser
DEFAULT_SIGNAL_URL = "http://192.168.100.1/cmSignalData.htm"
# Set up logging
log = logging.getLogger(__name__)
hdlr = logging.StreamHandler()
log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(log_formatter)
log.addHandler(hdlr)
def set_log_level(verbosity):
"""
Set the log level based on an integer between 0 and 2
"""
log_level = logging.WARNING # default
if verbosity == 1:
log_level = logging.INFO
elif verbosity >= 2:
log_level = logging.DEBUG
log.setLevel(level=log_level)
def get_options():
"""
Returns the options and arguments passed to this script on the commandline
@return: (options,args)
"""
usage = "usage: %prog [options] file1 file2 ..."
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 (specify multiple times for more)")
options, args = parser.parse_args()
return (options, args)
def main(args):
(options, args) = get_options()
set_log_level(options.verbose)
log.debug("Starting with options: %s" % (options))
timestamp = datetime.now().isoformat()
try:
r = requests.get(options.signal_url)
soup = BeautifulSoup(r.content, 'html.parser')
except:
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]
res[heading] = {}
if heading:
for table_row in table.find_all("tr"):
if table_row.th:
continue
row_data = []
for s in table_row.stripped_strings:
if s.startswith("The Downstream Power Level reading is a"):
continue
row_data.append(s)
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:]))
@jeffp123
Copy link
Author

Output looks like this:

{"timestamp": "2015-08-20T20:29:43.386229", "res": {"Signal Stats (Codewords)": {"Total Uncorrectable Codewords": ["2859", "1101", "2811", "1778"], "Total Correctable Codewords": ["473", "261", "307", "340"], "Channel ID": ["201", "202", "203", "204"], "Total Unerrored Codewords": ["629643765", "628717510", "628715909", "628717051"]}, "Upstream ": {"Symbol Rate": ["5.120 Msym/sec", "5.120 Msym/sec", "5.120 Msym/sec"], "Ranging Status": ["Success", "Success", "Success"], "Ranging Service ID": ["1979", "1979", "1979"], "Upstream Modulation": ["[3] QPSK", "[3] 64QAM", "[3] QPSK", "[3] 64QAM", "[3] QPSK", "[3] 64QAM"], "Power Level": ["39 dBmV", "41 dBmV", "40 dBmV"], "Frequency": ["23700000 Hz", "36500000 Hz", "30100000 Hz"], "Channel ID": ["3", "1", "2"]}, "Downstream ": {"Downstream Modulation": ["QAM256", "QAM256", "QAM256", "QAM256"], "Power Level": ["-5 dBmV", "-5 dBmV", "-6 dBmV", "-6 dBmV"], "Frequency": ["711000000 Hz", "717000000 Hz", "723000000 Hz", "729000000 Hz"], "Signal to Noise Ratio": ["37 dB", "37 dB", "37 dB", "36 dB"], "Channel ID": ["201", "202", "203", "204"]}}}

@jeffp123
Copy link
Author

You could use this with a tool like jq to filter for the information that you want. I hope someone other than me finds this useful :)

@robbiet480
Copy link

Made Python3 and PEP 8 fixes in my fork, as well as fixed an issue that caused Downstream and Upstream headings to output with an extra space at the end.

@rietta
Copy link

rietta commented Jan 6, 2018

Sweet! I just said to myself that I should write a script like this to sample the status of my modem. And you've already done it! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment