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

用法:

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

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

sudo dmidecode -t 1



(2)获取主板信息:

sudo dmidecode -t 2



(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免密:请参照 https://gaoming.blog.csdn.net/article/details/88993044

通过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服务器硬件信息的更多相关文章

  1. Python 通过wmi获取Window服务器硬件信息

    通过pip install wmi安装wmi 查看cpu序列号: wmic cpu get processorid 查看主板序列号: wmic baseboard get serialnumber 查 ...

  2. 运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程,把获取的信息存入数据库

    运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程 有关前两篇的链接: 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 运 ...

  3. 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本、内核、当前时间

    申请博客有一段时间了,然而到现在还一篇没有写过..... 主要因为没有想到需要写些什么,最近在学习Python语言,照着书上看了看最基础的东西,发现根本看不进去,而且光看的话今天看了觉得都理解懂了,过 ...

  4. 运用Python语言编写获取Linux基本系统信息(二):文件系统使用情况获取

    本文跟着上一篇文章继续写,上一篇文章的链接 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 一.随便说说 获取文件系统使用情况的思路和上一篇获取主要系统是 ...

  5. 使用 dmidecode 查看Linux服务器信息

    使用 dmidecode 查看Linux服务器信息 来源  http://www.laozuo.org/6682.html 对于大部分普通服务器用户来说,我们选择VPS.服务器产品的时候比较关心的是产 ...

  6. 使用 python 收集获取 Linux 系统主机信息

    使用 python 代码收集主机的系统信息,主要:主机名称.IP.系统版本.服务器厂商.型号.序列号.CPU信息.内存等系统信息. #!/usr/bin/env python #encoding: u ...

  7. 使用Python收集获取Linux系统主机信息

    爬虫代理IP由芝麻HTTP服务供应商提供 使用 python 代码收集主机的系统信息,主要:主机名称.IP.系统版本.服务器厂商.型号.序列号.CPU信息.内存等系统信息. #!/usr/bin/en ...

  8. 用Python获取Linux资源信息的三种方法

    方法一:psutil模块 #!usr/bin/env python # -*- coding: utf-8 -*- import socket import psutil class NodeReso ...

  9. 用python实现批量获取Linux主机简要信息并保存到Excel中 unstable 1.1

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- #filename get_linux_info.py #获取Linux主机的信息 # titles=[' ...

随机推荐

  1. [CSP-S模拟测试]:旅行计划(分块+DP)

    题目传送门(内部题83) 输入格式 第一行两个整数$n,m$ 接下来$m$行,每行三个整数,$u,v,w$,表示从$u$到$v$有一条权值为$w$的边 接下来一行有一个整数$q$,表示$q$天 接下来 ...

  2. enum简单使用

    前台传入weightCode :1/2/3/4,获取不同的区间0~10 10~50 50~100 100~999999 0~999999 public void setWeight(){ this.m ...

  3. http常用标签

    HTML标签大全(常用)   文本标记语言,即HTML(Hypertext Markup Language),是用于描述网页文档的一种标记语言. HTML之所以称为超文本标记语言,是因为文本中包含了所 ...

  4. Python的 counter内置函数,统计文本中的单词数量

    counter是 colletions内的一个类 可以理解为一个简单的计数 import collections str1=['a','a','b','d'] m=collections.Counte ...

  5. 如何在maven项目中引用领一个项目

    1 有两个项目 maven01  和maven 02,想在maven 02中引用maven01的方法,该如何操作呢 maven01中Factory类中的方法 public class Factory ...

  6. $.ajax函数调接口,报异常No converter found for return value of type: class java.util.ArrayList

    接口正常执行,返回给前端后报服务器500异常,异常详情: org.springframework.http.converter.HttpMessageNotWritableException: No ...

  7. Python 笔试集(1):关于 Python 链式赋值的坑

    前言 Python 的链式赋值是一种简易型批量赋值语句,一行代码即可为多个变量同时进行赋值. 例如: x = y = z = 1 链式赋值是一种非常优雅的赋值方式,简单.高效且实用.但同时它也是一个危 ...

  8. python学习之文件读写操作

    open函数 在使用文件之前,需要先打开,即使用open函数 如: files=open("文件路径","操作方式") 解释如下: (1.files:为文件对象 ...

  9. Ajax请求Json数据,报500错误,后台没有错误日志。

    post请求:http://localhost:9080/DataDiscoveryWeb/issueformcount/queryIssueTendencyDetail.xhtml?jobId=86 ...

  10. C 语言中的关键字 - 数据类型、数据修饰符及逻辑结构

    C 语言中有 32 个关键字.这是留个编译器用的特殊字符串,用户不可以使用. 特殊关键字 sizeof 和 return 是 C 语言中的两个特殊关键字. sizeof sizeof 用于计算变量所占 ...