Table of contents
1. Demand
Validation needs to be done so that the code only runs on a specific server. This requires obtaining the machine code that uniquely identifies the machine.
2. Solve
2.1. Infeasible solutions
Most of the Internet says uuid
to obtain the MAC address of the device through the module. However, the MAC address will change after the python code is re-run, and cannot be uniquely identified.
2.2. Feasible solutions
Use dmidecode
the command to get a specific serial number or UUID.
For dmidecode
a detailed description of the command, please refer to the blog "(Summary) A Tool for Obtaining Detailed Hardware Information under Linux: Detailed Explanation of the Dmidecode Command" .
Here, I choose the serial number of the motherboard of the machine as the unique identification code, and the acquisition command is as follows
sudo dmidecode -s baseboard-serial-number
The python code is as follows
import os
# 获得主板sn号
def getSN():
sn = os.popen("sudo dmidecode -s baseboard-serial-number | awk '{print $0}'").readlines()
return sn[0].strip()
print("当前机器的主板序列号:", getSN())