You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3 KiB
Python

import hashlib
import logging
import os
import socket
import subprocess
import uuid
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
def get_system_uuid():
# 获取系统uuid
# 这个 UUID 是与硬件相关的,因此即使在 Docker 容器中,它也应该是唯一的,可以用来标识宿主机,而不是容器本身。
with open("/sys/class/dmi/id/product_uuid", "r") as f:
host_uuid = f.read().strip()
return host_uuid
def get_docker_container_id():
# 获取当前 Docker 容器的 ID
cmd = "cat /proc/self/cgroup"
output = os.popen(cmd)
rests = output.readlines()
container_message = rests[-1]
if not container_message:
container_id = "abc"
else:
container_id = container_message.strip().split("docker/")[-1]
return container_id