golang get hardware info (uuid/hwid)

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

golang get hardware info (uuid/hwid)

问题

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

package main

import (
	"fmt"
	"os/exec"
	"strings"
)

func main() {
	cmd := exec.Command("wmic", "csproduct", "get", "uuid")
	output, err := cmd.Output()
	if err != nil {
		fmt.Println("命令执行出错:", err)
		return
	}

	hwid := strings.TrimSpace(strings.Split(string(output), "\n")[1])
	fmt.Println(hwid)

	// 将hwid分配给uuid变量
	uuid := hwid
	fmt.Println(uuid)
}

请注意,此代码假设您的操作系统上已安装并配置了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:

import subprocess

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

print(hwid)

Ouput:

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的解决方案:

package main

import (
	"bytes"
	"fmt"
	"os/exec"
)

func main() {

	const xx = "cmd.exe"

	var stdout bytes.Buffer
	cmd := exec.Command(xx, "/c", "wmic csproduct get uuid")
	cmd.Stdout = &stdout
	cmd.Run()

	out := stdout.String()

	fmt.Println(out)

}

输出:

UUID
9F23624C-33F1-3244-A2ZD-ABF6CC8E5FB5


英文:

I found a solution using, os/exec:

package main

import (
	"bytes"
	"fmt"
	"os/exec"
)

func main() {

	const xx = "cmd.exe"

	var stdout bytes.Buffer
	cmd := exec.Command(xx, "/c", "wmic csproduct get uuid")
	cmd.Stdout = &stdout
	cmd.Run()

	out := stdout.String()

	fmt.Println(out)

}

Output:

UUID
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:

确定