-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
84 lines (70 loc) · 2.38 KB
/
utils.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
import subprocess
def get_cpu_load_line():
cmd = "top -bn1 | grep load | awk '{printf \"cpu load: %.2f\", $(NF-2)}'"
out = subprocess.check_output(cmd, shell=True)
return out.decode('utf-8')
def get_cpu_temp_line():
cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk '{printf \"cpu temp: %3.1fc\", $1/1000}'"
out = subprocess.check_output(cmd, shell=True)
return out.decode('utf-8')
def get_mem_line():
cmd = "free -m | awk 'NR==2{printf \"ram: %s/%sMB\", $3,$2}'"
out = subprocess.check_output(cmd, shell=True)
return out.decode('utf-8')
def get_disk_line():
cmd = "df -h | awk '$NF==\"/\"{printf \"disk: %d/%dGB\", $3,$2}'"
out = subprocess.check_output(cmd, shell=True)
return out.decode('utf-8')
def get_ip_lines():
cmd = 'ip -4 -br addr'
out = subprocess.check_output(cmd, shell=True)
return out.decode('utf-8').split('\n')
def get_saved_connections():
cmd = 'nmcli -t c show'
out = subprocess.check_output(cmd, shell=True)
out = out.decode('utf-8')
lines = out.split('\n')
parsed = []
for line in lines:
if not line:
continue
line = line.replace(r'\:', '-')
line = line.replace(r'\\', '-')
fields = line.split(':')
name, uuid, _, dev = fields
d = {}
d['name'] = name
d['uuid'] = uuid
d['up'] = 1 if dev else 0
parsed.append(d)
return parsed
def get_wifi_aps():
cmd = 'nmcli -t device wifi list'
out = subprocess.check_output(cmd, shell=True)
out = out.decode('utf-8')
lines = out.split('\n')
parsed = []
for line in lines:
line = line.strip()
if not line:
continue
line = line.replace(r'\:', '-')
line = line.replace(r'\\', '-')
print("haha", line)
fields = line.split(':')
in_use, ssid, _, _, _, strength, _, _ = fields
d = {}
d['in_use'] = 1 if in_use.strip() else 0
d['strength'] = int(strength)
d['ssid'] = ssid
parsed.append(d)
return parsed
def run_cmd_with_timeout(cmd, timeout=None):
try:
out = subprocess.check_output(cmd, shell=True, timeout=timeout)
out = out.decode('utf-8')
return (0, out)
except subprocess.TimeoutExpired as e:
return (-1, 'time out')
except subprocess.CalledProcessError as e:
return (e.returncode, 'cmd problem')