dmidecode收集系统信息
dmidecode收集系统信息
[root@133systeminformation]#vimdmidecode_1.py#!/usr/bin/envpythonfromsubprocessimportPopen,PIPEp=Popen(['dmidecode'],stdout=PIPE)data=p.stdoutline_s=[]#定义一个空列表dmi={}#定义一个空字典a=True#设置标志位a=Truewhilea:line=data.readline()ifline.startswith('SystemInformation'):#判断以SystemInformation开头的段,whileTrue:line=data.readline()ifline=='\n':#取该段落,直到有空行a=Falsebreakelse:line_s.append(line)dmi_dic=dict([i.strip().split(':')foriinline_s])#把空格和换行符删除dmi['Manufacturer']=dmi_dic['Manufacturer'].strip()#打印key=Manufacturer的键值对printdmiprint{'\n'*20}fork,vindmi_dic.items():#打印以SystemInformation开头的段,以key、value打印出来dmi[k]=v.strip()printdmi[root@133systeminformation]#pythondmidecode_1.py{'Manufacturer':'DellInc.'}set(['\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n']){'SKUNumber':'NotSpecified','UUID':'4C4C4544-0048-4210-8044-B4C04F543258','Family':'NotSpecified','SerialNumber':'4HBDT2X','Version':'NotSpecified','ProductName':'PowerEdgeR710','Wake-upType':'PowerSwitch','Manufacturer':'DellInc.'}
注意:在Python里,以下这些对象相当于布尔值的False
空列表([] )
空元组(() )
空字典({} )
空字符串('' )
零值(0 )
特殊对象None
对象False
[root@133systeminformation]#catdmidecode_2.py#!/usr/bin/envpythonfromsubprocessimportPopen,PIPEdefgetDmi():p=Popen(['dmidecode'],stdout=PIPE)data=p.stdout.read()returndatadefparseDmi(data):lines=[]line_in=Falsedmi_list=[iforiindata.split('\n')ifi]forlineindmi_list:ifline.startswith('SystemInformation'):line_in=Truecontinueifline_in:ifnotline[0].strip():lines.append(line)else:breakreturnlinesdefdmiDic():dmi_dic={}data=getDmi()lines=parseDmi(data)dic=dict([i.strip().split(':')foriinlines])dmi_dic['vendor']=dic['Manufacturer']returndmi_dicif__name__=='__main__':printdmiDic()[root@133systeminformation]#pythondmidecode_2.py{'vendor':'DellInc.'}In[1]:a=''#a的值是null,nota=TrueIn[2]:notaOut[2]:TrueIn[3]:a=''#a的值是空格或者\t,a!=null,nota=FalseIn[4]:notaOut[4]:FalseIn[5]:a='\t'In[6]:aOut[6]:'\t'In[7]:notaOut[7]:FalseIn[8]:a='\t'In[9]:aOut[9]:'\t'In[10]:a.strip()Out[10]:''In[11]:nota.strip()Out[11]:TrueIn[12]:a=''In[13]:notaOut[13]:FalseIn[14]:a.strip()Out[14]:''In[15]:nota.strip()Out[15]:True
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。