Get Linux System Information using Python (via the Dmidecode command)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.