#!/usr/bin/env python3 # -*- coding: utf-8 -*- import subprocess import re dict_sn_productName = {} def getDmidecode(): # #variable 'out' is subprocess output info top_info = subprocess.Popen(["dmidecode", "-t", "system"], stdout=subprocess.PIPE) out, err = top_info.communicate() #output info get from console has many unicode escape character ,such as x1b(Bx1b[mx1b[39;49mx1b[K x1b(Bx1b[m #use decode('unicode-escape') to process out_info = out.decode('unicode-escape') #print(out_info) lines = [] lines = out_info.split(' ') #return out_info # get serial number if 'Serial Number' in out_info: serialNumberSearch = re.compile('Serial Number: (.+?) ') serialNumber = serialNumberSearch.findall(out_info)[0] dict_sn_productName['serialNumber'] = serialNumber # get Product Name if 'Product Name' in out_info: productNameSearch = re.compile('Product Name: (.+?) ') productName = productNameSearch.findall(out_info)[0] dict_sn_productName['productName'] = productName getDmidecode() print (dict_sn_productName)
output:
[root@mgt MgmtStatus]#./dmidecode.py {'productName':'IBM System x3650 M4: -[7915R7Q]-','serialNumber':'06BAFCD'} [root@mgt MgmtStatus]#