dmidecode.py (python3_dmidecode)

#!/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(B\x1b[m\x1b[39;49m\x1b[K\n\x1b(B\x1b[m
    #use decode('unicode-escape') to process
    out_info = out.decode('unicode-escape')
    #print(out_info)
    lines = []
    lines = out_info.split('\n')
    #return out_info
    # get serial number
    if 'Serial Number' in out_info:
        serialNumberSearch = re.compile('Serial Number: (.+?)\n')
        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: (.+?)\n')
        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]#

 

 
 





posted @ 2016-07-08 16:27  乾坤颠倒  阅读(642)  评论(0编辑  收藏  举报