import subprocess import socket import hashlib import uuid import logging def get_cpu_id(): p = subprocess.Popen(["dmidecode -t 4 | grep ID"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) data = p.stdout lines = [] while True: line = str(data.readline(), encoding="utf-8") if line == '\n': break if line: d = dict([line.strip().split(': ')]) lines.append(d) else: break return lines def get_board_serialnumber(): p = subprocess.Popen(["dmidecode -t 2 | grep Serial"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) data = p.stdout lines = [] while True: line = str(data.readline(), encoding="utf-8") if line == '\n': break if line: d = dict([line.strip().split(': ')]) lines.append(d) else: break return lines def get_identify_code(): mac = uuid.UUID(int=uuid.getnode()).hex[-12:] mac_addr = ":".join([mac[e:e + 2] for e in range(0, 11, 2)]) host_name = socket.getfqdn(socket.gethostname()) cpu_ids = get_cpu_id() serialnumbers = get_board_serialnumber() s = "" if mac_addr: s += mac_addr if host_name: s += host_name if cpu_ids: for cpu in cpu_ids: s += cpu["ID"] if serialnumbers: for number in serialnumbers: s += number["Serial Number"] logging.info(s) code = hashlib.new('md5', s.encode("utf8")).hexdigest() return code