Package: dmiparser
python-dmiparserAboutThis parsedmidecodeoutput to JSON text.InstallationPyPIpip3install-UdmiparserRPMgitclonehttps://github.com/Arondight/python-dmiparser.gitcd./python-dmiparser/
python3./setup.pybdist--format=rpm
sudodnfinstall./dist/dmiparser-*.noarch.rpmTip: Requires therpm-buildpackage in your Linux distribution.UsagePython 3 scriptDmiParserThis accepts astr(with the output ofdmidecode) as argument and converts it to JSON text.#!/usr/bin/env python3importjsonfromdmiparserimportDmiParserfromfunctoolsimportpartialdefreport(*args:str)->None:"""report texts with format@param args: text string"""br=lambdae:print("-"*e)brn=partial(br,80)brn()foreinargs:print(e)brn()if"__main__"==__name__:text=("# dmidecode 3.0\n""Getting SMBIOS data from sysfs.\n""SMBIOS 2.7 present.\n""\n""Handle 0x0003, DMI type 2, 17 bytes\n""Base Board Information\n""\tManufacturer: Intel Corporation\n""\tProduct Name: S2600WT2R\n""\tVersion: H21573-372\n""\tSerial Number: BQWL81150522\n""\tAsset Tag: Base Board Asset Tag\n""\tFeatures:\n""\t\tBoard is a hosting board\n""\t\tBoard is replaceable\n""\tLocation In Chassis: Part Component\n""\tChassis Handle: 0x0000\n""\tType: Motherboard\n""\tContained Object Handles: 0\n""\n")# parser = DmiParser(text)parser=DmiParser(text,sort_keys=True,indent=2)parsedStr=str(parser)# get strparsedObj=json.loads(str(parser))# get objectreport(parsedStr,parsedObj)DmiDecoder (the default wrapper)This rundmidecodeand converting the output of the command to JSON text.fromdmiparser.dmidecoderimportDmiDecoderfromfunctoolsimportpartialdefreport(*args:str)->None:"""report texts with format@param args: text string"""br=lambdae:print("-"*e)brn=partial(br,80)brn()foreinargs:print(e)brn()defgetCpuInfo(dmidecoder)->str:"""Get CPU information, will return text like below.CPU1:Family: XeonVersion: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHzVoltage: 1.8 VSpeed: 2200 MHz/4000 MHzStatus: Populated, EnabledCore: 10/10Thread: 20CPU2:Family: XeonVersion: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHzVoltage: 1.8 VSpeed: 2200 MHz/4000 MHzStatus: Populated, EnabledCore: 10/10Thread: 20@param: dmidecode: DmiDecode object@return: text of CPU information"""text=""forid,nameindmidecoder.sections:defgetFirst(*args):vals=dmidecoder.getProp(*args,id=id,name=name)returnvals[0]iflen(vals)>0elseNonetext+="{}:\n".format(getFirst("Socket Designation"))text+="\tFamily:{}\n".format(getFirst("Family"))text+="\tVersion:{}\n".format(getFirst("Version"))text+="\tVoltage:{}\n".format(getFirst("Voltage"))text+="\tSpeed:{}/{}\n".format(getFirst("Current Speed"),getFirst("Max Speed"))text+="\tStatus:{}\n".format(getFirst("Status"))text+="\tCore:{}/{}\n".format(getFirst("Core Enabled"),getFirst("Core Count"))text+="\tThread:{}\n".format(getFirst("Thread Count"))returntextif"__main__"==__name__:# dmidecoder = DmiDecoder()dmidecoder4=DmiDecoder("-t 4",sort_keys=True,indent=2)# Type 4 is Processorreport(dmidecoder4.text,str(dmidecoder4.data),getCpuInfo(dmidecoder4))Tip: Superuser permissions are required here to rundmidecode.Executable commanddmiparserThis read output ofdmidecodefrom pipe and print it as JSON text.sudodmidecode|dmiparser
sudodmidecode-t4|dmiparser--formatsudodmidecode>/tmp/dmidecode.txt
dmiparser</tmp/dmidecode.txtTip: you can rundmiparsermodule as a script (usepython3 -m dmiparserinstead ofdmiparsercommand).dmidecoderThis rundmidecodeand print the output as JSON text.sudodmidecoder
sudodmidecoder--arguments"-t 4"--formatTip: you can rundmiparser.dmidecodermodule as a script (usepython3 -m dmiparser.dmidecoderinstead
ofdmidecodercommand).DevelopmentTesttoxFormatblack-l120./dmiparser/./tests/LicenseMIT LICENSE.