dmidecode收集系统信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dmidecode收集系统信息相关的知识,希望对你有一定的参考价值。

dmidecode收集系统信息


[[email protected] systeminformation]# vim dmidecode_1.py
#!/usr/bin/env python                                                                                                                                                              
                                                                                                                                                                                   
from subprocess import Popen,PIPE                                                                                                                                                  
p = Popen([‘dmidecode‘],stdout=PIPE)                                                                                                                                               
data=p.stdout                                                                                                                                                                      
line_s = []   #定义一个空列表                                                                                                                                                                  
dmi = {}     #定义一个空字典                                                                                                                                                                      
                                                                                                                                                                                   
a = True              #设置标志位 a = True                                                                                                                                                             
while a:                                                                                                                                                                           
    line = data.readline()                                                                                                                                                         
    if line.startswith(‘System Information‘): #判断以System Information开头的段,                                                                                                                              
        while True:                                                                                                                                                                
            line = data.readline()                                                                                                                                                 
            if line == ‘\n‘:           #取该段落,直到有空行                                                                                                                                            
                a = False                                                                                                                                                          
                break                                                                                                                                                              
            else:                                                                                                                                                                  
                line_s.append(line)                                                                                                                                                
                                                                                                                                                                                   
                                                                                                                                                                                   
dmi_dic =  dict([i.strip().split(‘:‘) for i in line_s])  #把空格和换行符删除

                                                                                                                          
dmi[‘Manufacturer‘] = dmi_dic[‘Manufacturer‘].strip()  #打印key= Manufacturer的键值对                                                                                                                            
print dmi                                                                                                                                                                          
print {‘\n‘*20}                                                                                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                   
for k,v in dmi_dic.items():   #打印以System Information开头的段,以key、value打印出来                                                                                                                                                     
    dmi[k] = v.strip()                                                                                                                                                             
print dmi   

[[email protected] systeminformation]# python dmidecode_1.py 
{‘Manufacturer‘: ‘Dell Inc.‘}

set([‘\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n‘])

{‘SKU Number‘: ‘Not Specified‘, ‘UUID‘: ‘4C4C4544-0048-4210-8044-B4C04F543258‘, ‘Family‘: ‘Not Specified‘, ‘Serial Number‘: ‘4HBDT2X‘, ‘Version‘: ‘Not Specified‘, ‘Product Name‘: 
‘PowerEdge R710‘, ‘Wake-up Type‘: ‘Power Switch‘, ‘Manufacturer‘: ‘Dell Inc.‘}


注意:在Python里,以下这些对象相当于布尔值的False

空列表([] )

空元组(() )

空字典({} )

空字符串(‘‘ )

零值(0 )

特殊对象None

对象False

本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1884514

以上是关于dmidecode收集系统信息的主要内容,如果未能解决你的问题,请参考以下文章

Linux硬件信息采集

收集系统信息

使用 python 获取 Linux 系统信息(通过dmidecode命令)

Python收集主机信息

python 获取Dmidecode 输出的系统硬件信息

使用Dmidecode获取硬件信息使用实例