gitee.com/quant1x/gox@v1.21.2/devices/cpu_darwin.go (about) 1 //go:build !windows 2 3 package devices 4 5 import ( 6 "gitee.com/quant1x/gox/api" 7 "os" 8 "os/exec" 9 "strings" 10 ) 11 12 const ( 13 EnvPath = "PATH" 14 ) 15 16 // CpuPhysicalID CPU序列号 17 func CpuPhysicalID() (PhysicalID string, err error) { 18 PhysicalID = "Unknown" 19 path := os.Getenv(EnvPath) 20 path += ":" + "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" 21 err = os.Setenv(EnvPath, path) 22 if err != nil { 23 return 24 } 25 cmd := exec.Command("system_profiler", "SPHardwareDataType") 26 out, err := cmd.CombinedOutput() 27 if err != nil { 28 return PhysicalID, err 29 } 30 infos := api.Bytes2String(out) 31 tmpArray := strings.Split(infos, "\n") 32 for _, line := range tmpArray { 33 line := strings.TrimSpace(line) 34 key, value, found := strings.Cut(line, ":") 35 if found { 36 key = strings.TrimSpace(key) 37 value = strings.TrimSpace(value) 38 if len(key) == 0 || len(value) == 0 { 39 continue 40 } 41 key = strings.ReplaceAll(key, " ", "") 42 key = strings.ToLower(key) 43 44 if strings.HasPrefix(key, "serialnumber") { 45 PhysicalID = value 46 break 47 } 48 } 49 } 50 return PhysicalID, nil 51 }