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

方式1:

 #!/usr/bin/python
#encoding: utf-8 from subprocess import Popen, PIPE p = Popen(['dmidecode'], stdout = PIPE)
data = p.stdout
lines = []
dicts = {} while True:
line = data.readline()
if line.startswith('System Information'):
while True:
line = data.readline()
if line == '\n':
break
else:
lines.append(line)
break d = dict([i.strip().split(':') for i in lines]) #for k,v in dicts.items():
# dicts[k] = v.strip()
dicts['Manufacturer'] = d['Manufacturer'].strip()
dicts['Product Name'] = d['Product Name'].strip()
dicts['Serial Number'] = d['Serial Number'].strip()
print dicts

方式2:

 #!/usr/bin/python
#encoding: utf-8 from subprocess import Popen, PIPE def getDmi():
p = Popen(['dmidecode'], stdout = PIPE)
data = p.stdout.read()
return data def parseDmi(data):
lines = []
line_in = False
dmi_list = [i for i in data.split('\n') if i]
for line in dmi_list:
if line.startswith('System Information'):
line_in = True
continue
if line_in:
if not line[0].strip():
lines.append(line)
else:
break
return lines def dimDic():
dmi_dic = {}
data = getDmi()
lines = parseDmi(data)
dic = dict([i.strip().split(': ') for i in lines])
dmi_dic['vendor'] = dic['Manufacturer']
dmi_dic['product'] = dic['Product Name']
dmi_dic['sn'] = dic['Serial Number'] return dmi_dic if __name__ == '__main__':
print dimDic()

使用 python 获取 Linux 系统信息(通过dmidecode命令)的更多相关文章

  1. 使用 Python 获取 Linux 系统信息

    探索platform模块 platform模块在标准库中,它有很多运行我们获得众多系统信息的函数.让我们运行Python解释器来探索它们中的一些函数,那就从platform.uname()函数开始吧: ...

  2. 【转】 使用 Python 获取 Linux 系统信息

    在本文中,我们将会探索使用Python编程语言工具来检索Linux系统各种信息.走你. 哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相 ...

  3. 使用 python 获取 Linux 的 IP 信息(通过 ifconfig 命令)

    我们可以使用 python 代码通过调用 ifconfig 命令来获取 Linux 主机的 IP 相关信息,包括:网卡名称.MAC地址.IP地址等. 第一种实现方式: #!/usr/bin/pytho ...

  4. 使用Python获取Linux系统的各种信息

    哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份解释不同之处的备选代码 ...

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

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

  6. Python学习之旅:使用Python实现Linux中的ls命令

    一.写在前面 前几天在微信上看到这样一篇文章,链接为:https://mp.weixin.qq.com/s/rl6Sgv3uk_IpoFAx6cWa8w,在这篇文章中,有这样一段话,吸引了我的注意: ...

  7. python获取Linux发行版名称

    我必须从Python脚本中获取Linux发行版名称.dist平台模块中有一个方法: import platform platform.dist() 但在我的Arch Linux下它返回: >&g ...

  8. Python脚本获取Linux系统信息

    # -*- coding:utf-8 -*- import os import subprocess import re import hashlib #对字典取子集 def sub_dict(for ...

  9. python3 获取Linux系统信息

    系统信息 import platform platform.uname() platform.architecture() CPU信息 /proc/cpuinfo文件包含了系统处理器单元的信息. #! ...

随机推荐

  1. C中的fseek函数使用

    函数名:fseek函数 头文件:#include<stdio.h> 功能:把与fp有关的文件位置指针放到一个指定位置. 格式:  int fseek(FILE *stream, long ...

  2. mousewheel事件的兼容方法

    在垂直方向上滚动页面时,会触发mousewheel事件,这个事件会在任何元素上触发,最终都会冒泡到document(IE8)或window(IE9+及其他主流现代浏览器)对象. 在给元素指定mouse ...

  3. 简单粗暴,详细得不要不要的 JavaWeb快速入门实例(1)

    额,有些标题党的嫌疑,小细节不用在意哈... 前端时间我在写一个系列,是关于JavaWeb的一个入门级项目实战,我的初衷就是打算写给初学者的,希望能对他们有所帮助. 这段时间博主也接触了一些事情,感觉 ...

  4. [Web API] Web API 2 深入系列(4) Action的选择

    目录 ApiController HttpActionDescriptor IHttpActionSelector ApiController 在上节中,讲到如何选择并激活对应的IHttpContro ...

  5. Not a git repository (or any of the parent directories): .git

    今天准备在win10上面安装git,想把代码发布到github中,按照教程的方法一步一步下来,当配置完ssh和用户名,邮箱之后,发现下拉不下来github中的代码,出现如下错误. 说是没有发现仓储,很 ...

  6. mobileControls与移动控件适配

    此配置节的作用在于指定各种控件在不同类型的移动设备显示的适配器,以达到适应各种设备不同的展示形式.例子如下, <mobileControls sessionStateHistorySize=&q ...

  7. Parallel并行编程初步

    Parallel并行编程可以让我们使用极致的使用CPU.并行编程与多线程编程不同,多线程编程无论怎样开启线程,也是在同一个CPU上切换时间片.而并行编程则是多CPU核心同时工作.耗时的CPU计算操作选 ...

  8. 谈I/O模型

    一个IO操作涉及两个系统对象: 调用这个IO的用户Process/Thread 系统内核 - System Kernel 一个具体的Read操作包括两个阶段: 内核等待数据准备就绪:Waiting f ...

  9. UIAlertController 部分用法及属性

    //创建UIAlertController:初始化UIAlertController 需要使用alertControllerWithTitle UIAlertController *alertCont ...

  10. 关于C#操作防火墙,阻止程序联网

    //开启服务.开启防火墙 public void OpenFileWall() { // 1. 判断当前系统为XP或Win7 RegistryKey rk = Registry.LocalMachin ...