Files
AILab/sentinel/pkg/device/deviceid.go
T
2025-12-29 16:30:55 +08:00

41 lines
740 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package device
import (
"net"
"os"
"strings"
)
// GetDeviceID 返回本机原始唯一IDLinux /etc/machine-id 或 hostname+MAC
func GetDeviceID() string {
// 尝试读取 Linux /etc/machine-id
if data, err := os.ReadFile("/etc/machine-id"); err == nil {
s := strings.TrimSpace(string(data))
if s != "" {
return s
}
}
// fallback: hostname + first non零MAC
hn, _ := os.Hostname()
mac := getFirstMac()
return hn + "|" + mac
}
func getFirstMac() string {
ifaces, err := net.Interfaces()
if err != nil {
return ""
}
for _, it := range ifaces {
if len(it.HardwareAddr) == 0 {
continue
}
mac := it.HardwareAddr.String()
if mac != "" && mac != "00:00:00:00:00:00" {
return mac
}
}
return ""
}