通过 dmidecode 命令可以获取到 Linux 系统的包括 BIOS、 CPU、内存等系统的硬件信息,这里使用 python 代码来通过调用 dmidecode 命令来获取 Linux 必要的系统信息

用法:

1、 dmidecode -t [类型代码或名称 ] 指令

(1)获取系统信息,例如:

​sudo dmidecode -t 1​

Python 通过dmidecode获取Linux服务器硬件信息_ide

(2)获取主板信息:

​sudo dmidecode -t 2​

Python 通过dmidecode获取Linux服务器硬件信息_ios_02

(3)获取CPU ID

​sudo dmidecode -t 4 | grep ID​

(4)获取系统序列号

​sudo dmidecode | grep Serial​

附:

dmidecode -t 指令参数参考

Type   Information
────────────────────────────────────────────
0 BIOS
1 System
2 Baseboard
3 Chassis
4 Processor
5 Memory Controller
6 Memory Module
7 Cache
8 Port Connector
9 System Slots
10 On Board Devices

11 OEM Strings
12 System Configuration Options
13 BIOS Language
14 Group Associations
15 System Event Log
16 Physical Memory Array
17 Memory Device
18 32-bit Memory Error
19 Memory Array Mapped Address
20 Memory Device Mapped Address
21 Built-in Pointing Device
22 Portable Battery
23 System Reset
24 Hardware Security
25 System Power Controls
26 Voltage Probe
27 Cooling Device
28 Temperature Probe
29 Electrical Current Probe
30 Out-of-band Remote Access
31 Boot Integrity Services
32 System Boot
33 64-bit Memory Error
34 Management Device
35 Management Device Component
36 Management Device Threshold Data
37 Memory Channel
38 IPMI Device
39 Power Supply
40 Additional Information
41 Onboard Devices Extended Information
42 Management Controller Host Interface

2、dmidecode -s [关键字] 指令

例如,查看处理器生产厂家:

​sudo dmidecode -s processor-manufacturer​

附可查询的关键字:

bios-vendor, bios-version, bios-release-date,  
system-manufacturer, system-product-name,system-version, system-serial-number, system-uuid,
baseboard-manufacturer, baseboard-product-name, baseboard-version, baseboard-serial-number, baseboard-asset-tag,
chassis-manufacturer, chassis-type, chassis-version, chassis-serial-number, chassis-asset-tag,
processor-family, processor-manufacturer, processor-version, processor-frequency

需要配置sudo免密:请参照 ​javascript:void(0)">​javascript:void(0)​

通过Python获取cpu id、主办序列号、内存数量

#!/usr/bin/python
# encoding: utf-8

import subprocess


def getCpuId():
p = subprocess.Popen(["sudo dmidecode -t 4 | grep ID"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE , stderr=subprocess.PIPE)
data = p.stdout
lines = []
while True:
line = str(data.readline(), encoding="utf-8")
if line == '\n':
break
if line:
d = dict([line.strip().split(': ')])
lines.append(d)
else:
break
return lines


def getBaseboardSerialnumber ():
p = subprocess.Popen(["sudo dmidecode -t 2 | grep Serial"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE , stderr=subprocess.PIPE)
data = p.stdout
lines = []
while True:
line = str(data.readline(), encoding="utf-8")
if line == '\n':
break
if line:
d = dict([line.strip().split(': ')])
lines.append(d)
else:
break
return lines


def memory ():
p = subprocess.Popen(["sudo dmidecode -t memory | grep 'Number Of Devices'"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE , stderr=subprocess.PIPE)
data = str(p.stdout.read(), encoding="utf-8")
d = dict([data.strip().split(': ')])
return d


if __name__ == '__main__':
print (getCpuId())
print (getBaseboardSerialnumber())
print (memory())

Python 通过dmidecode获取Linux服务器硬件信息_ios_03