Last active
September 15, 2017 07:53
-
-
Save blha303/dba82817d51d282d988664fc12646e5f to your computer and use it in GitHub Desktop.
A script for getting connection statistics from a Vivid Wireless-supplied Huawei B315s-607. Other models may have a similar feature.
This file contains hidden or 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
$ python3 usage.py | |
Current Connect Time: 23:52:44 | |
Current Upload: 930.35MB | |
Current Download: 30.65GB | |
Current Download Rate: 610.00KB/s | |
Current Upload Rate: 6.35MB/s | |
Total Upload: 934.36MB | |
Total Download: 30.67GB | |
Total Connect Time: 01:26:02 | |
$ python3 usage.py -v | |
<?xml version="1.0" encoding="UTF-8"?> | |
<response> | |
<CurrentConnectTime>85966</CurrentConnectTime> | |
<CurrentUpload>930354382</CurrentUpload> | |
<CurrentDownload>30646273305</CurrentDownload> | |
<CurrentDownloadRate>71276</CurrentDownloadRate> | |
<CurrentUploadRate>2555</CurrentUploadRate> | |
<TotalUpload>934367315</TotalUpload> | |
<TotalDownload>30672352534</TotalDownload> | |
<TotalConnectTime>87964</TotalConnectTime> | |
<showtraffic>1</showtraffic> | |
</response> | |
Current Connect Time: 23:52:46 | |
Current Upload: 930.35MB | |
Current Download: 30.65GB | |
Current Download Rate: 71.28MB/s | |
Current Upload Rate: 2.56MB/s | |
Total Upload: 934.37MB | |
Total Download: 30.67GB | |
Total Connect Time: 01:26:04 |
This file contains hidden or 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 | |
import requests | |
from xml.etree import ElementTree | |
from sys import argv | |
ROUTER_IP = "192.168.0.1" | |
def b2B(num_of_bytes, start=-1): | |
if type(num_of_bytes) not in (float, int): | |
num_of_bytes = float(num_of_bytes) | |
opts, i = "KMGTP", start | |
while num_of_bytes > 1000: | |
num_of_bytes = num_of_bytes / 1000 | |
i += 1 | |
return "{:.2f}{}B".format(num_of_bytes, opts[i]) | |
def hms(s): | |
m,s = divmod(s,60) | |
h,m = divmod(m,60) | |
d,h = divmod(h,24) | |
y,d = divmod(d,365) | |
return y,d,h,m,s | |
def camel2sp(word): | |
return ''.join(map(lambda x: x if x.islower() else " "+x, word)) | |
i = requests.get("http://{}/html/home.html".format(ROUTER_IP)) | |
d = requests.get("http://{}/api/monitoring/traffic-statistics".format(ROUTER_IP), headers={"Cookie": i.headers["Set-Cookie"]}).text | |
if len(argv) > 1 and argv[1] == "-v": print(d) | |
d = ElementTree.fromstring(d) | |
for e in d: | |
if e.tag in ["CurrentConnectTime", "TotalConnectTime"]: | |
out = "{}: {}".format(camel2sp(e.tag), ":".join(str(x).zfill(2) for x in hms(int(e.text)) if x)) | |
elif e.tag in ["CurrentUpload", "CurrentDownload", "TotalUpload", "TotalDownload"]: | |
out = "{}: {}".format(camel2sp(e.tag), b2B(e.text)) | |
elif e.tag in ["CurrentUploadRate", "CurrentDownloadRate"]: | |
out = "{}: {}/s".format(camel2sp(e.tag), b2B(float(e.text), 0)) | |
else: | |
continue | |
print(out.strip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment