-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.1s.py
executable file
·117 lines (95 loc) · 2.88 KB
/
net.1s.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
import psutil
import time
import subprocess
COLOR_HIGH = "#cc575d"
COLOR_MEDIUM = "#d19a66"
COLOR_NORMAL = "#ffffff"
SCALE_BYTES = 1024
NET_THRESHOLD_MEDIUM = 1024 * 1024 * 10
NET_THRESHOLD_HIGH = 1024 * 1024 * 50
PING_THRESHOLD_MEDIUM = 20
PING_THRESHOLD_HIGH = 100
# Wireguard VPN prefixes
VPN_PREFIXES = ["wg", "dts"]
# Addresses to ping
PING = ["google.com"]
####
def humanize(bytes):
suffix = "B"
if bytes > SCALE_BYTES:
bytes /= SCALE_BYTES
suffix = "K"
if bytes > SCALE_BYTES:
bytes /= SCALE_BYTES
suffix = "M"
if bytes > SCALE_BYTES:
bytes /= SCALE_BYTES
suffix = "G"
return "%4.f%s" % (bytes, suffix)
def get_color(num, medium_threshold, high_threshold):
if num >= high_threshold:
return COLOR_HIGH
if num >= medium_threshold:
return COLOR_MEDIUM
return COLOR_NORMAL
def ping(server, count=1, wait_sec=1):
cmd = "ping -c {} -W {} {}".format(count, wait_sec, server).split(" ")
try:
output = subprocess.check_output(cmd).decode().strip()
lines = output.split("\n")
timing = lines[-1].split()[3].split("/")
return {
"to": server,
"type": "rtt",
"min": float(timing[0]),
"avg": float(timing[1]),
"max": float(timing[2]),
"mdev": float(timing[3]),
}
except Exception:
return {"avg": -1, "to": server}
####
count = psutil.net_io_counters()
recv, sent = count.bytes_recv, count.bytes_sent
time.sleep(1)
count = psutil.net_io_counters()
recv, sent = count.bytes_recv - recv, count.bytes_sent - sent
net_if_stats = psutil.net_if_stats()
is_connected_vpn = False
for key in net_if_stats:
for prefix in VPN_PREFIXES:
if prefix in key:
is_connected_vpn = True
PINGS = {}
LAST_AVG_PING = 0
for to_ping in PING:
PINGS[to_ping] = ping(to_ping)
LAST_AVG_PING = PINGS[to_ping]["avg"]
print(
"{down}{up}{ping}{vpn}|font=monospace".format(
down="👇<span color='%s'>%s</span>"
% (get_color(recv, NET_THRESHOLD_MEDIUM, NET_THRESHOLD_HIGH), humanize(recv)),
up="☝<span color='%s'>%s</span>"
% (get_color(sent, NET_THRESHOLD_MEDIUM, NET_THRESHOLD_HIGH), humanize(sent)),
vpn=(" 🔒" if is_connected_vpn else ""),
ping=" 🔗<span color='%s'>%4.dms</span>"
% (
get_color(LAST_AVG_PING, PING_THRESHOLD_MEDIUM, PING_THRESHOLD_HIGH),
LAST_AVG_PING,
),
)
)
print("---")
for key in net_if_stats:
if net_if_stats[key].isup:
print(f"Interface {key} is up|font=monospace")
for ping in PINGS:
print(
"Ping to %s: <span color='%s'>%4.dms</span>|font=monospace"
% (
PINGS[ping]["to"],
get_color(PINGS[ping]["avg"], PING_THRESHOLD_MEDIUM, PING_THRESHOLD_HIGH),
PINGS[ping]["avg"],
)
)