Get connection status for Franklin Wireless cellular hotspots.
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
#!/usr/bin/env python3 | |
"""Get connection status for Franklin Wireless cellular hotspots. | |
Example script to store login information: | |
```sh | |
#!/bin/sh | |
set -e | |
# Be sure to use a strong, unique password if you are storing it in plaintext. | |
hostname=192.168.0.1 | |
password=admin | |
command "$(basename -- "$0" .local)" --password-stdin "$hostname" "$@" <<EOF | |
$password | |
EOF | |
``` | |
""" | |
import argparse | |
import getpass | |
import json | |
import os | |
import sys | |
from typing import * | |
import requests # apt/dnf: python3-requests; apk: py3-requests; pip3: requests | |
def get_debug_json(hostname: str, password: str) -> str: | |
def to_json(dict: dict) -> str: | |
return json.dumps(dict, separators=(',', ':')) + "\n" | |
session = requests.Session() | |
headers = {"Content-Type": "application/json; charset=UTF-8"} | |
# get session cookie | |
r = session.post( | |
f"http://{hostname}/cgi-bin/general_monitor.cgi", | |
data=to_json({"command": "load"}), | |
headers=headers, | |
) | |
rj = r.json() | |
if not rj.get("data", {}).get("login_available"): | |
raise RuntimeError("login not available") | |
# log in | |
r = session.post( | |
f"http://{hostname}/cgi-bin/login.cgi", | |
data=to_json({"command": "log_in", "params": {"password": password}}), | |
headers=headers, | |
) | |
rj = r.json() | |
if rj.get("result", "") != "S_LOGIN": | |
raise RuntimeError(f"could not login: {rj.get('result', '')}: {rj.get('msg', '')}") | |
# get debug JSON | |
r = session.post( | |
f"http://{hostname}/cgi-bin/about.debug-lte_engineering.cgi", | |
data=to_json({"command": "load", "params": None}), | |
headers=headers, | |
) | |
rj = r.json() | |
if rj.get("result", "") != "S_OK": | |
raise RuntimeError(f"could not get debug JSON: {rj.get('result', '')}: {rj.get('msg', '')}") | |
result = json.dumps(rj.get("data", rj), indent=4) | |
# try to log out | |
try: | |
r = session.post( | |
f"http://{hostname}/cgi-bin/login.cgi", | |
data=to_json({"command": "log_out"}), | |
headers=headers, | |
) | |
except requests.exceptions.RequestException: | |
pass | |
return result | |
def main(argv: List[str]) -> int: | |
p = argparse.ArgumentParser( | |
description=__doc__.strip().splitlines()[0].strip(), | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
) | |
p.add_argument("hostname", | |
help="the hostname or IP address of the hotspot") | |
p.add_argument("--json", "-j", action="store_true", | |
help="print JSON data as received from the hotspot") | |
p.add_argument("--password-stdin", action="store_true", | |
help="read the password from standard input") | |
try: | |
options = p.parse_args(argv[1:]) | |
except SystemExit as exc: | |
return exc.code | |
hostname = options.hostname | |
if options.password_stdin: | |
password = input() | |
else: | |
password = getpass.getpass("Hotspot password: ") | |
try: | |
debug_json = get_debug_json(hostname, password) | |
except (RuntimeError, requests.exceptions.RequestException) as exc: | |
print(f"error: {exc}", file=sys.stderr) | |
return 1 | |
if options.json: | |
print(debug_json) | |
else: | |
debug_data = json.loads(debug_json) | |
fields = dict( | |
power_mode="Power mode", | |
srv_state="Service state", | |
connection="Connection", | |
_0="", | |
plmn_name="PLMN name", | |
mcc="MCC", | |
mnc="MNC", | |
roam="Roaming", | |
_1="", | |
technology="Technology", | |
band="Band", | |
band_width="Band width", | |
cell_global_id="Cell global ID", | |
_2="", | |
rsrp="RSRP", | |
rsrq="RSRQ", | |
rssi="RSSI", | |
sinr="SINR", | |
snr="SNR", | |
tx_power="Transmit power", | |
) | |
for key, name in fields.items(): | |
if key.startswith("_"): | |
print() | |
else: | |
print(f"{name}: {debug_data.get(key, '')}") | |
return 0 | |
SAMPLE_DATA = { | |
"band": "Band 71", | |
"band_width": "10MHz", | |
"cell_global_id": "000(00000000)", | |
"dl_earfcn": "00000", | |
"imei": "000000000000000", | |
"imsi": "000000000000000", | |
"ipv4_address": "192.0.2.1", | |
"ipv6_address": "2001:db8::1", | |
"last_error": " ", | |
"mcc": "310", | |
"mnc": "260", | |
"pdn_type": " ", | |
"physical_cell_id": " ", | |
"plmn_name": "T-Mobile", | |
"plmn_search": " ", | |
"plmn_selected": "310260", | |
"power_mode": "Online", | |
"roam": "0", | |
"rrc_state": " ", | |
"rsrp": "-100", | |
"rsrq": "-20", | |
"rssi": "-60", | |
"sinr": " ", | |
"snr": "-10.0", | |
"srv_state": "In Service", | |
"supported_technology": "LTE+UMTS", | |
"technology": "LTE", | |
"tx_power": "16.0", | |
"ul_earfcn": "000000" | |
} | |
if __name__ == "__main__": | |
try: | |
sys.exit(main(sys.argv)) | |
except KeyboardInterrupt: | |
pass |
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
#!/bin/sh | |
set -e | |
# Be sure to use a strong, unique password if you are storing it in plaintext. | |
hostname=192.168.0.1 | |
password=admin | |
command "$(basename -- "$0" .local)" --password-stdin "$hostname" "$@" <<EOF | |
$password | |
EOF |
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
Power mode: Online | |
Service state: In Service | |
Connection: Connected | |
PLMN name: T-Mobile | |
MCC: 310 | |
MNC: 260 | |
Roaming: 0 | |
Technology: LTE | |
Band: Band 71 | |
Band width: 10MHz | |
Cell global ID: 000(00000000) | |
RSRP: -100 | |
RSRQ: -20 | |
RSSI: -60 | |
SINR: | |
SNR: -10.0 | |
Transmit power: 16.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment