-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmidecode.py
164 lines (140 loc) · 4.1 KB
/
dmidecode.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Sources:
# https://pypi.org/project/dmidecode/
# http://pleasedonttouchthescreen.blogspot.com/2012/05/dmidecode-211-for-windows.html
from __future__ import print_function
import os, sys, platform, urllib
__version__ = "0.9.0"
TYPE = {
0: 'bios',
1: 'system',
2: 'base board',
3: 'chassis',
4: 'processor',
7: 'cache',
8: 'port connector',
9: 'system slot',
10: 'on board device',
11: 'OEM strings',
#13: 'bios language',
15: 'system event log',
16: 'physical memory array',
17: 'memory device',
19: 'memory array mapped address',
24: 'hardware security',
25: 'system power controls',
27: 'cooling device',
32: 'system boot',
41: 'onboard device',
}
def parse_dmi(content):
"""
Parse the whole dmidecode output.
Returns a list of tuples of (type int, value dict).
"""
info = []
lines = iter(content.strip().splitlines())
while True:
try:
line = next(lines)
except StopIteration:
break
if line.startswith('Handle 0x'):
typ = int(line.split(',', 2)[1].strip()[len('DMI type'):])
if typ in TYPE:
info.append((TYPE[typ], _parse_handle_section(lines)))
return info
def _parse_handle_section(lines):
"""
Parse a section of dmidecode output
* 1st line contains address, type and size
* 2nd line is title
* line started with one tab is one option and its value
* line started with two tabs is a member of list
"""
data = {
'_title': next(lines).rstrip(),
}
for line in lines:
line = line.rstrip()
if line.startswith('\t\t'):
if type(data[k]) != list:
data[k] = []
data[k].append(line.lstrip())
elif line.startswith('\t'):
k, v = [i.strip() for i in line.lstrip().split(':', 1)]
if v:
data[k] = v
else:
data[k] = []
else:
break
return data
def profile():
if os.isatty(sys.stdin.fileno()):
content = _get_output()
else:
content = sys.stdin.read()
info = parse_dmi(content)
_show(info)
return info
def _get_output():
import subprocess
if platform.system() == 'Windows':
output = subprocess.check_output(".\dmidecode.exe")
else:
try:
output = subprocess.check_output(
'PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin '
'sudo dmidecode', shell=True)
print('output')
except Exception as e:
print(e, file=sys.stderr)
if str(e).find("command not found") == -1:
print("please install dmidecode", file=sys.stderr)
print("e.g. sudo apt install dmidecode",file=sys.stderr)
sys.exit(1)
return output.decode()
def _show(info):
def _get(i):
return [v for j, v in info if j == i]
system = _get('system')[0]
print ('%s %s (SN: %s, UUID: %s)' % (
system['Manufacturer'],
system['Product Name'],
system['Serial Number'],
system['UUID'],
))
for cpu in _get('processor'):
#fix for output in virtual machine environments
if 'Thread Count' in cpu:
threads = cpu['Thread Count']
else:
threads = "-"
print ('%s %s %s (Thead: %s)' % (
cpu['Manufacturer'],
cpu['Family'],
cpu['Max Speed'],
#cpu['Core Count'],
threads,
))
cnt, total, unit = 0, 0, None
for mem in _get('memory device'):
if mem['Size'] == 'No Module Installed':
continue
i, unit = mem['Size'].split()
cnt += 1
total += int(i)
print ('%d memory stick(s), %d %s in total' % (
cnt,
total,
unit,
))
bios = _get('bios')[0]
print ('BIOS: %s v.%s %s Systemversion: %s' % (
bios['Vendor'],
bios['Version'],
bios['Release Date'],
system['Version']
))
if __name__ == '__main__':
profile()