Steven Smith revised this gist . Go to revision
2 files changed, 50 insertions, 17 deletions
output.txt
| @@ -1,9 +1,33 @@ | |||
| 1 | 1 | $ python3 usage.py | |
| 2 | - | CurrentConnectTime: 12:47:44 | |
| 3 | - | CurrentUpload: 817.37MB | |
| 4 | - | CurrentDownload: 27.28GB | |
| 5 | - | CurrentDownloadRate: 3.50MB/s | |
| 6 | - | CurrentUploadRate: 409.60KB/s | |
| 7 | - | TotalUpload: 821.38MB | |
| 8 | - | TotalDownload: 27.31GB | |
| 9 | - | TotalConnectTime: 13:21:02 | |
| 2 | + | Current Connect Time: 23:52:44 | |
| 3 | + | Current Upload: 930.35MB | |
| 4 | + | Current Download: 30.65GB | |
| 5 | + | Current Download Rate: 610.00KB/s | |
| 6 | + | Current Upload Rate: 6.35MB/s | |
| 7 | + | Total Upload: 934.36MB | |
| 8 | + | Total Download: 30.67GB | |
| 9 | + | Total Connect Time: 01:26:02 | |
| 10 | + | ||
| 11 | + | ||
| 12 | + | $ python3 usage.py -v | |
| 13 | + | <?xml version="1.0" encoding="UTF-8"?> | |
| 14 | + | <response> | |
| 15 | + | <CurrentConnectTime>85966</CurrentConnectTime> | |
| 16 | + | <CurrentUpload>930354382</CurrentUpload> | |
| 17 | + | <CurrentDownload>30646273305</CurrentDownload> | |
| 18 | + | <CurrentDownloadRate>71276</CurrentDownloadRate> | |
| 19 | + | <CurrentUploadRate>2555</CurrentUploadRate> | |
| 20 | + | <TotalUpload>934367315</TotalUpload> | |
| 21 | + | <TotalDownload>30672352534</TotalDownload> | |
| 22 | + | <TotalConnectTime>87964</TotalConnectTime> | |
| 23 | + | <showtraffic>1</showtraffic> | |
| 24 | + | </response> | |
| 25 | + | ||
| 26 | + | Current Connect Time: 23:52:46 | |
| 27 | + | Current Upload: 930.35MB | |
| 28 | + | Current Download: 30.65GB | |
| 29 | + | Current Download Rate: 71.28MB/s | |
| 30 | + | Current Upload Rate: 2.56MB/s | |
| 31 | + | Total Upload: 934.37MB | |
| 32 | + | Total Download: 30.67GB | |
| 33 | + | Total Connect Time: 01:26:04 | |
vivid-huawei.py
| @@ -1,13 +1,14 @@ | |||
| 1 | 1 | #!/usr/bin/env python3 | |
| 2 | 2 | import requests | |
| 3 | 3 | from xml.etree import ElementTree | |
| 4 | + | from sys import argv | |
| 4 | 5 | ||
| 5 | - | ROUTER_IP = "192.168.17.1" | |
| 6 | + | ROUTER_IP = "192.168.0.1" | |
| 6 | 7 | ||
| 7 | - | def b2B(num_of_bytes): | |
| 8 | + | def b2B(num_of_bytes, start=-1): | |
| 8 | 9 | if type(num_of_bytes) not in (float, int): | |
| 9 | 10 | num_of_bytes = float(num_of_bytes) | |
| 10 | - | opts, i = "KMGTP", -1 | |
| 11 | + | opts, i = "KMGTP", start | |
| 11 | 12 | while num_of_bytes > 1000: | |
| 12 | 13 | num_of_bytes = num_of_bytes / 1000 | |
| 13 | 14 | i += 1 | |
| @@ -20,12 +21,20 @@ def hms(s): | |||
| 20 | 21 | y,d = divmod(d,365) | |
| 21 | 22 | return y,d,h,m,s | |
| 22 | 23 | ||
| 24 | + | def camel2sp(word): | |
| 25 | + | return ''.join(map(lambda x: x if x.islower() else " "+x, word)) | |
| 26 | + | ||
| 23 | 27 | i = requests.get("http://{}/html/home.html".format(ROUTER_IP)) | |
| 24 | - | d = ElementTree.fromstring(requests.get("http://{}/api/monitoring/traffic-statistics".format(ROUTER_IP), headers={"Cookie": i.headers["Set-Cookie"]}).text) | |
| 28 | + | d = requests.get("http://{}/api/monitoring/traffic-statistics".format(ROUTER_IP), headers={"Cookie": i.headers["Set-Cookie"]}).text | |
| 29 | + | if len(argv) > 1 and argv[1] == "-v": print(d) | |
| 30 | + | d = ElementTree.fromstring(d) | |
| 25 | 31 | for e in d: | |
| 26 | 32 | if e.tag in ["CurrentConnectTime", "TotalConnectTime"]: | |
| 27 | - | print("{}: {}".format(e.tag, ":".join(str(x).zfill(2) for x in hms(int(e.text)) if x))) | |
| 28 | - | if e.tag in ["CurrentUpload", "CurrentDownload", "TotalUpload", "TotalDownload"]: | |
| 29 | - | print("{}: {}".format(e.tag, b2B(e.text))) | |
| 30 | - | if e.tag in ["CurrentUploadRate", "CurrentDownloadRate"]: | |
| 31 | - | print("{}: {}/s".format(e.tag, b2B(float(e.text)*1024))) | |
| 33 | + | out = "{}: {}".format(camel2sp(e.tag), ":".join(str(x).zfill(2) for x in hms(int(e.text)) if x)) | |
| 34 | + | elif e.tag in ["CurrentUpload", "CurrentDownload", "TotalUpload", "TotalDownload"]: | |
| 35 | + | out = "{}: {}".format(camel2sp(e.tag), b2B(e.text)) | |
| 36 | + | elif e.tag in ["CurrentUploadRate", "CurrentDownloadRate"]: | |
| 37 | + | out = "{}: {}/s".format(camel2sp(e.tag), b2B(float(e.text), 0)) | |
| 38 | + | else: | |
| 39 | + | continue | |
| 40 | + | print(out.strip()) | |
Steven Smith revised this gist . Go to revision
2 files changed, 40 insertions
output.txt(file created)
| @@ -0,0 +1,9 @@ | |||
| 1 | + | $ python3 usage.py | |
| 2 | + | CurrentConnectTime: 12:47:44 | |
| 3 | + | CurrentUpload: 817.37MB | |
| 4 | + | CurrentDownload: 27.28GB | |
| 5 | + | CurrentDownloadRate: 3.50MB/s | |
| 6 | + | CurrentUploadRate: 409.60KB/s | |
| 7 | + | TotalUpload: 821.38MB | |
| 8 | + | TotalDownload: 27.31GB | |
| 9 | + | TotalConnectTime: 13:21:02 | |
vivid-huawei.py(file created)
| @@ -0,0 +1,31 @@ | |||
| 1 | + | #!/usr/bin/env python3 | |
| 2 | + | import requests | |
| 3 | + | from xml.etree import ElementTree | |
| 4 | + | ||
| 5 | + | ROUTER_IP = "192.168.17.1" | |
| 6 | + | ||
| 7 | + | def b2B(num_of_bytes): | |
| 8 | + | if type(num_of_bytes) not in (float, int): | |
| 9 | + | num_of_bytes = float(num_of_bytes) | |
| 10 | + | opts, i = "KMGTP", -1 | |
| 11 | + | while num_of_bytes > 1000: | |
| 12 | + | num_of_bytes = num_of_bytes / 1000 | |
| 13 | + | i += 1 | |
| 14 | + | return "{:.2f}{}B".format(num_of_bytes, opts[i]) | |
| 15 | + | ||
| 16 | + | def hms(s): | |
| 17 | + | m,s = divmod(s,60) | |
| 18 | + | h,m = divmod(m,60) | |
| 19 | + | d,h = divmod(h,24) | |
| 20 | + | y,d = divmod(d,365) | |
| 21 | + | return y,d,h,m,s | |
| 22 | + | ||
| 23 | + | i = requests.get("http://{}/html/home.html".format(ROUTER_IP)) | |
| 24 | + | d = ElementTree.fromstring(requests.get("http://{}/api/monitoring/traffic-statistics".format(ROUTER_IP), headers={"Cookie": i.headers["Set-Cookie"]}).text) | |
| 25 | + | for e in d: | |
| 26 | + | if e.tag in ["CurrentConnectTime", "TotalConnectTime"]: | |
| 27 | + | print("{}: {}".format(e.tag, ":".join(str(x).zfill(2) for x in hms(int(e.text)) if x))) | |
| 28 | + | if e.tag in ["CurrentUpload", "CurrentDownload", "TotalUpload", "TotalDownload"]: | |
| 29 | + | print("{}: {}".format(e.tag, b2B(e.text))) | |
| 30 | + | if e.tag in ["CurrentUploadRate", "CurrentDownloadRate"]: | |
| 31 | + | print("{}: {}/s".format(e.tag, b2B(float(e.text)*1024))) | |