golang get hardware info (uuid/hwid)

huangapple go评论120阅读模式
英文:

golang get hardware info (uuid/hwid)

问题

你可以使用Go语言中的exec包来执行命令并获取输出。以下是在Go中复制该功能并将其分配给变量的示例代码:

  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strings"
  6. )
  7. func main() {
  8. cmd := exec.Command("wmic", "csproduct", "get", "uuid")
  9. output, err := cmd.Output()
  10. if err != nil {
  11. fmt.Println("命令执行出错:", err)
  12. return
  13. }
  14. hwid := strings.TrimSpace(strings.Split(string(output), "\n")[1])
  15. fmt.Println(hwid)
  16. // 将hwid分配给uuid变量
  17. uuid := hwid
  18. fmt.Println(uuid)
  19. }

请注意,此代码假设您的操作系统上已安装并配置了wmic命令。

英文:

I am trying get a unique hardware info such as the uuid of a device for a client based application that will have an authentication process.

In python it would be something like:

  1. import subprocess
  2. hwid = str(subprocess.check_output('wmic csproduct get uuid')).split('\\r\\n')[1].strip('\\r').strip()
  3. print(hwid)

Ouput:

  1. 9F23624C-33F1-3244-A2ZD-ABF6CC8E5FB5

How do can I replicate this function in go, and assign it to a variable ? uuid := xxx

答案1

得分: -1

我找到了一个使用os/exec的解决方案:

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os/exec"
  6. )
  7. func main() {
  8. const xx = "cmd.exe"
  9. var stdout bytes.Buffer
  10. cmd := exec.Command(xx, "/c", "wmic csproduct get uuid")
  11. cmd.Stdout = &stdout
  12. cmd.Run()
  13. out := stdout.String()
  14. fmt.Println(out)
  15. }

输出:

  1. UUID
  2. 9F23624C-33F1-3244-A2ZD-ABF6CC8E5FB5
英文:

I found a solution using, os/exec:

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os/exec"
  6. )
  7. func main() {
  8. const xx = "cmd.exe"
  9. var stdout bytes.Buffer
  10. cmd := exec.Command(xx, "/c", "wmic csproduct get uuid")
  11. cmd.Stdout = &stdout
  12. cmd.Run()
  13. out := stdout.String()
  14. fmt.Println(out)
  15. }

Output:

  1. UUID
  2. 9F23624C-33F1-3244-A2ZD-ABF6CC8E5FB5

huangapple
  • 本文由 发表于 2022年5月24日 05:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/72355088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定