Linux obtains the unique machine number of the server through python

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 uuidto 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 dmidecodethe command to get a specific serial number or UUID.

For dmidecodea 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())

Guess you like

Origin blog.csdn.net/qq_30841655/article/details/128318069