Files
a13labs.infra/tools/read_system_id/main.go
T

80 lines
1.6 KiB
Go

package main
import (
"bufio"
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
)
func getModelName() string {
file, err := os.Open("/proc/cpuinfo")
if err != nil {
return ""
}
defer file.Close()
scanner := bufio.NewScanner(file)
re := regexp.MustCompile(`^model name\s*:\s*(.*)$`)
for scanner.Scan() {
line := scanner.Text()
if matches := re.FindStringSubmatch(line); matches != nil {
return matches[1]
}
}
return ""
}
func getEth0Mac() string {
out, err := exec.Command("ip", "link").Output()
if err != nil {
return ""
}
re := regexp.MustCompile(`^(\d+):\s+([a-zA-Z0-9]+):.*<.*>\s+link/ether\s+([0-9a-f:]{17})`)
matches := re.FindAllStringSubmatch(string(out), -1)
fmt.Println("Matches:", matches)
if len(matches) == 0 {
return ""
}
first_device := matches[0][2]
fmt.Println("First device:", first_device)
// Get the MAC address of the first device
out, err = exec.Command("ip", "link", "show", first_device).Output()
if err != nil {
return ""
}
re = regexp.MustCompile(`ether\s+([0-9a-f:]{17})`)
match := re.FindStringSubmatch(string(out))
if match != nil {
return match[1]
}
return ""
}
func getSystemUUID() string {
out, err := exec.Command("sudo", "dmidecode", "-s", "system-uuid").Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(out))
}
func md5sum(s string) string {
h := md5.New()
io.WriteString(h, s)
return hex.EncodeToString(h.Sum(nil))
}
func main() {
uuid := getModelName()
uuid += getEth0Mac()
uuid += getSystemUUID()
fmt.Println(uuid)
hash := md5sum(uuid)
fmt.Println(hash)
}