成都网站建设设计

将想法与焦点和您一起共享

dmidecode收集系统信息-创新互联

dmidecode收集系统信息

创新互联公司长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为吉林企业提供专业的成都网站设计、做网站吉林网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。
[root@133 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   

[root@133 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

[root@133 systeminformation]# cat dmidecode_2.py 
#!/usr/bin/env python
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 dmiDic():
    dmi_dic = {}
    data = getDmi()
    lines = parseDmi(data)
    dic = dict ([i.strip().split(':')for i in lines])
    dmi_dic['vendor'] = dic['Manufacturer']
    return dmi_dic

if __name__ == '__main__':
    print dmiDic()
    
[root@133 systeminformation]# python dmidecode_2.py 
{'vendor': ' Dell Inc.'}


In [1]: a=''  #a的值是null,not a = True

In [2]: not a
Out[2]: True

In [3]: a=' '#a的值是空格或者\t, a!=null, not a = False

In [4]: not a
Out[4]: False

In [5]: a='\t'

In [6]: a
Out[6]: '\t'

In [7]: not a
Out[7]: False

In [8]: a='\t'

In [9]: a
Out[9]: '\t'

In [10]: a.strip()
Out[10]: ''

In [11]: not a.strip()
Out[11]: True



In [12]: a='   '

In [13]: not a
Out[13]: False

In [14]: a.strip()
Out[14]: ''

In [15]: not a.strip()
Out[15]: True

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站栏目:dmidecode收集系统信息-创新互联
文章路径:http://chengdu.cdxwcx.cn/article/cechdh.html