最近有需求,需要产生一个与电脑绑定的唯一ID,网络上有很多常规方案,比如:
- MAC地址: 很不错,但是会遇上多个网卡情况
- CPU SN/ID: 现在CPU大多只有Family ID,没有SN,所以同型号的CPU会重复
- 硬盘ID:有可能唯一,但是现在一般多个硬盘
- 主板ID?这是个好主意,但是主板有很多信息,哪个可以呢?一般可以考虑主板SN和UUID
- OS UUID:包含安装时产生的UUID以及产品UUID(Windows平台),对于Linux等似乎不适用。
考虑到跨平台的需求,目前通过dmi获取主板BIOS信息最为靠谱,然后可以考虑添加MAC或者硬盘ID。
参考别人的实现:
https://blog.csdn.net/gobest28/article/details/84561602
https://www.jianshu.com/p/2e7ce2946b6b
通过跨平台方案直接获取主板bmi信息,得到CPU和主板特征:(MAC和硬盘需要其他方式获取)
package main
import (
"fmt"
"os"
"github.com/yumaojun03/dmidecode"
)
func checkError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// https://blog.csdn.net/gobest28/article/details/84561602
// https://www.jianshu.com/p/2e7ce2946b6b
func main() {
// 主板smBIOS UUID
// MachineGUID
// MAC地址
// 硬盘序列号
dmi, err := dmidecode.New()
checkError(err)
infos, err := dmi.ALL()
infos.Print()
}
大概的输出如下:
Handle e, DMI type 0, 24 bytes
BIOS Information
Vendor: LENOVO
Version: N1EET98W (1.71 )
Release Date: 12/06/2022
Address: 0xE0000
Runtime Size: 128 kB
ROM Size: 0 kB
Characteristics:
PCI is supported
PNP is supported
BIOS is upgradeable
BIOS shadowing is allowed
Boot from CD is supported
Selectable boot is supported
EDD is supported
3.5"/720 kB floppy services are supported (int 13h)
Print screen service is supported (int 5h)
8042 keyboard services are supported (int 9h)
Serial services are supported (int 14h)
Printer services are supported (int 17h)
CGA/mono video services are supported (int 10h)
ACPI is supported
I2O boot is supported
BIOS boot specification is supported
Targeted content distribution is supported
UEFI is supported
System is a virtual machine
Handle f, DMI type 1, 27 bytes
System Information
Manufacturer: LENOVO
Product Name: 20EQS0T70G
Version: ThinkPad P50
Serial Number: PC0X7A0M
UUID: 33EC83CC-315A-11B2-A85C-E9065B388106
Wake-up Type: Power Switch
SKU Number: LENOVO_MT_20EQ_BU_Think_FM_ThinkPad P50
Family: ThinkPad P50
xxx,9Base Board Information
Manufacturer: LENOVO
Product Name: 20EQS0T70G
Version: SDK0J40675 WIN
Serial Number: L1HF88F00E2
Asset Tag: Not Available
Features:
Board is a hosting board
Board is replaceable
Location In Chassis: Not Available
Type: Motherboard
Chassis Information
Manufacturer: LENOVO
Type: Notebook
Lock: Not Present
Version: None
Serial Number: PC0X7A0M
Asset Tag: No Asset Information
Boot-up State: Unknown
Power Supply State: Unknown
Thermal State: Unknown
Security Status: Unknown
OEM Information: 0
Height: Not Specified
Number Of Power Cords: 0
Contained Elements: {0 0 0}
SKU Number: No Asset Information
Port Information
Internal Reference Designator: Not Available
Internal Connector Type: None
External Reference Designator: USB 1
External Connector Type: Access Bus (USB)
Type: USB
Port Information
Internal Reference Designator: Not Available
Internal Connector Type: None
External Reference Designator: USB 2
External Connector Type: Access Bus (USB)
Type: USB
Port Information
Internal Reference Designator: Not Available
Internal Connector Type: None
External Reference Designator: USB 3
External Connector Type: Access Bus (USB)
Type: USB
Port Information
Internal Reference Designator: Not Available
Internal Connector Type: None
External Reference Designator: Ethernet
External Connector Type: RJ-45
Type: Network Port
Port Information
Internal Reference Designator: Not Available
Internal Connector Type: None
External Reference Designator: Hdmi
External Connector Type: %!s(PANIC=String method: runtime error: index out of range [255] with length 41)
Type: Video Port
Port Information
Internal Reference Designator: Not Available
Internal Connector Type: None
External Reference Designator: Mini DisplayPort
External Connector Type: %!s(PANIC=String method: runtime error: index out of range [255] with length 41)
Type: Video Port
Port Information
Internal Reference Designator: Not Available
Internal Connector Type: None
External Reference Designator: Headphone/Microphone Combo Jack1
External Connector Type: Mini-jack (headphones)
Type: Audio Port
Processor Information
Socket Designation: U3E1
Processor Type: CentralProcessor
Family: Dual-Core Intel® Celeron® processor
Manufacturer: Intel(R) Corporation
ID: E3 06 05 00 FF FB EB BF
Version: Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz
Voltage: 1.0
External Clock: 100
Max Speed: 8300
Current Speed: 2700
Status: Populated,CPU Enabled
Upgrade: Unknown
L1 Cache Handle: 4
L2 Cache Handle: 5
L3 Cache Handle: 406
Serial Number: None
Asset Tag: None
Part Number: None
Core Count: 4
Core Enabled: 4
Thread Count: 8
Characteristics: 64-bit Capable,Multi-Core,Hardware Thread,Execute Protection,Enhanced Virtualization,Power/Performance Control
Family2: Dual-Core Intel® Celeron® processor
Cache Information
Socket Designation: L1 Cache
Configuration: Cache Configuration:
Level: Level1
Socketed: false
Location: Internal
Enabled: false
Mode: Write Back
Maximum Cache Size: 128 * 1K
Installed Size: 128 * 1K
SupportedSRAM Type: %!s(PANIC=String method: runtime error: index out of range [16] with length 8)
CurrentSRAM Type: %!s(PANIC=String method: runtime error: index out of range [16] with length 8)
Cache Speed: 0
Error Correction Type: Parity
System Cache Type: Data
Associativity: 16-way Set-Associative
Cache Information
Socket Designation: L1 Cache
Configuration: Cache Configuration:
Level: Level1
Socketed: false
Location: Internal
Enabled: false
Mode: Write Back
Maximum Cache Size: 128 * 1K
Installed Size: 128 * 1K
SupportedSRAM Type: %!s(PANIC=String method: runtime error: index out of range [16] with length 8)
CurrentSRAM Type: %!s(PANIC=String method: runtime error: index out of range [16] with length 8)
Cache Speed: 0
Error Correction Type: Parity
System Cache Type: Instruction
Associativity: 16-way Set-Associative
Cache Information
Socket Designation: L2 Cache
Configuration: Cache Configuration:
Level: Level2
Socketed: false
Location: Internal
Enabled: false
Mode: Write Back
Maximum Cache Size: 1024 * 1K
Installed Size: 1024 * 1K
SupportedSRAM Type: %!s(PANIC=String method: runtime error: index out of range [16] with length 8)
CurrentSRAM Type: %!s(PANIC=String method: runtime error: index out of range [16] with length 8)
Cache Speed: 0
Error Correction Type: Single-bit ECC
System Cache Type: Unified
Associativity: Fully Associative
Cache Information
Socket Designation: L3 Cache
Configuration: Cache Configuration:
Level: Level3
Socketed: false
Location: Internal
Enabled: false
Mode: Write Back
Maximum Cache Size: 8192 * 1K
Installed Size: 8192 * 1K
SupportedSRAM Type: %!s(PANIC=String method: runtime error: index out of range [16] with length 8)
CurrentSRAM Type: %!s(PANIC=String method: runtime error: index out of range [16] with length 8)
Cache Speed: 0
Error Correction Type: Multi-bit ECC
System Cache Type: Unified
Associativity: 12-way Set-Associative
Physcial Memory Array
Location: System board or motherboard
Use: System memory
Memory Error Correction: None
Maximum Capacity: 67108864
Memory Error Information Handle: 65534
Number of Memory Devices: 4
Extended Maximum Capacity: 0
Memory Device
Physical Memory Array Handle: 8
Memory Error Information Handle: 65534
Total Width: 64
Data Width: 64
Size: 8192
Form Factor: SODIMM
Device Set: 0
Device Locator: ChannelA-DIMM0
Bank Locator: BANK 0
Memory Type: DDR4
Type Detail: Synchronous
Speed: 2133
Manufacturer: Samsung
Serial Number: 4031B912
Asset Tag:
Part Number: M471A1K43CB1-CRC
Attributes: 1
Extended Size: 0
Configured Memory Clock Speed: 2133
Minimum voltage: 0
Maximum voltage: 0
Configured voltage: 0
Memory Device
Physical Memory Array Handle: 8
Memory Error Information Handle: 65534
Total Width: 0
Data Width: 0
Size: 0
Form Factor: Unknown
Device Set: 0
Device Locator: ChannelA-DIMM1
Bank Locator: BANK 1
Memory Type: Unknown
Type Detail:
Speed: 0
Manufacturer: Unknown
Serial Number: Unknown
Asset Tag:
Part Number: Unknown
Attributes: 0
Extended Size: 0
Configured Memory Clock Speed: 0
Minimum voltage: 0
Maximum voltage: 0
Configured voltage: 0
Memory Device
Physical Memory Array Handle: 8
Memory Error Information Handle: 65534
Total Width: 64
Data Width: 64
Size: 8192
Form Factor: SODIMM
Device Set: 0
Device Locator: ChannelB-DIMM0
Bank Locator: BANK 2
Memory Type: DDR4
Type Detail: Synchronous
Speed: 2133
Manufacturer: Samsung
Serial Number: 4031BBAD
Asset Tag:
Part Number: M471A1K43CB1-CRC
Attributes: 1
Extended Size: 0
Configured Memory Clock Speed: 2133
Minimum voltage: 0
Maximum voltage: 0
Configured voltage: 0
Memory Device
Physical Memory Array Handle: 8
Memory Error Information Handle: 65534
Total Width: 64
Data Width: 64
Size: 16384
Form Factor: SODIMM
Device Set: 0
Device Locator: ChannelB-DIMM1
Bank Locator: BANK 3
Memory Type: DDR4
Type Detail: Synchronous
Speed: 2133
Manufacturer: 0443
Serial Number: 1278952B
Asset Tag:
Part Number: RMSA3300MH78HBF-2666
Attributes: 2
Extended Size: 0
Configured Memory Clock Speed: 2133
Minimum voltage: 0
Maximum voltage: 0
Configured voltage: 0
System Slot 0
Slot Designation: Media Card Slot
Slot Type: Other
Slot Data Bus Width: Other
Current Usage: Available
Slot Length: Other
Slot ID: 0
Slot Characteristics1: Characteristics unknown.
Slot Characteristics2: Slot supports hot-plug devices.
Segment Group Number: 0
Bus Number: 0
Device/Function Number: 0
System Slot 0
Slot Designation: SmartCard Slot
Slot Type: Other
Slot Data Bus Width: Other
Current Usage: Available
Slot Length: Other
Slot ID: 0
Slot Characteristics1: Characteristics unknown.
Slot Characteristics2: Slot supports hot-plug devices.
Segment Group Number: 0
Bus Number: 0
Device/Function Number: 0
System Slot 0
Slot Designation: SimCard Slot
Slot Type: Other
Slot Data Bus Width: Other
Current Usage: Available
Slot Length: Other
Slot ID: 0
Slot Characteristics1: Characteristics unknown.
Slot Characteristics2: PCI slot supports Power Management Event (PME#) signal.
Segment Group Number: 0
Bus Number: 0
Device/Function Number: 0