英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论