Through the Dmidecode command can get to the Linux system including BIOS, CPU, memory and other systems hardware information, here using Python code to call the Dmidecode command to obtain Linux necessary system information, more information can be obtained in this way.
Mode 1:
1 #!/usr/bin/python2 #Encoding:utf-83 4 fromSubprocessImportPopen, PIPE5 6p = Popen (['Dmidecode'], stdout =PIPE)7data =P.stdout8Lines = []9Dicts = {}Ten One whileTrue: Aline =Data.readline () - ifLine.startswith ('System Information'): - whileTrue: theline =Data.readline () - ifline = ='\ n': - Break - Else: + Lines.append (line) - Break + AD = Dict ([I.strip (). Split (':') forIinchlines]) at - #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 () in PrintDicts
Mode 2:
1 #!/usr/bin/python2 #Encoding:utf-83 4 fromSubprocessImportPopen, PIPE5 6 defGetdmi ():7p = Popen (['Dmidecode'], stdout =PIPE)8data =P.stdout.read ()9 returnDataTen One defPARSEDMI (data): ALines = [] -Line_in =False -Dmi_list = [i forIinchData.split ('\ n')ifi] the forLineinchdmi_list: - ifLine.startswith ('System Information'): -Line_in =True - Continue + ifline_in: - if notLine[0].strip (): + Lines.append (line) A Else: at Break - returnLines - - defdimdic (): -Dmi_dic = {} -data =Getdmi () inLines =PARSEDMI (data) -DIC = Dict ([I.strip (). Split (': ') forIinchlines]) todmi_dic['Vendor'] = dic['manufacturer'] +dmi_dic['Product'] = dic['Product Name'] -dmi_dic['SN'] = dic['Serial Number'] the * returnDmi_dic $ Panax Notoginseng - if __name__=='__main__': the PrintDimdic ()
Get Linux System Information using Python (via the Dmidecode command)