如何在Golang中获取当前主机的唯一标识符?

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

how to get the unique identifier of the current host in golang?

问题

我想获取当前主机的唯一标识符,用作golang中的许可证名称。如何做到这一点?例如,像C语言中的:

gethostid() //可以获取主机ID
英文:

I want to get the unique identifier of the current host that is used as the license name in golang. How to do that ? For example, like C:

gethostid() //can get the host id

答案1

得分: 4

你可能想要获取machine-id

http://man7.org/linux/man-pages/man5/machine-id.5.html 上说:

> 机器ID通常在系统安装过程中从随机源生成,并在所有后续启动中保持不变。对于无状态系统,如果发现为空,则可以在早期引导期间在运行时生成。

> 机器ID不会因为本地或网络配置的更改或硬件的更换而改变。由于这一点以及其较长的长度,它是 POSIX 规定的 gethostid(3) 调用的更有用的替代品。

你可以在(最近的)Linux系统上获取machine-id

cat /etc/machine-id
# 或者
cat /var/lib/dbus/machine-id

大多数主要的操作系统都有唯一的主机标识符。但是,可能会存在非唯一的主机ID(由于镜像/克隆/备份恢复引起)。

你也可以查看我的golang包[machineid][1],了解实现细节,它适用于BSD、Linux、OS X和Windows,并且不需要管理员权限。

[1]: https://github.com/denisbrodbeck/machineid "machineid"

英文:

You probably want the machine-id.

http://man7.org/linux/man-pages/man5/machine-id.5.html says:

> The machine ID is usually generated from a random source during
system installation and stays constant for all subsequent boots.
Optionally, for stateless systems, it is generated during runtime at
early boot if it is found to be empty.

> The machine ID does not change based on local or network
configuration or when hardware is replaced. Due to this and its
greater length, it is a more useful replacement for the gethostid(3)
call that POSIX specifies.

You can get the machine-id on (recent) Linux systems with:

cat /etc/machine-id
# or
cat /var/lib/dbus/machine-id

Most major OSs have a unique host identifier. Still, there may be non-unique host IDs (caused by imaging/cloning/backup-restore).

You can also check out my golang package [machineid][1] for implementation details, which works on BSD, Linux, OS X and Windows and requires no admin privileges.

[1]: https://github.com/denisbrodbeck/machineid "machineid"

答案2

得分: 1

gethostid(3)是一个UNIX/BSD特定的libc函数。从/etc/hostid中读取在非UNIX系统上不起作用,并且不是平台无关的。

由于Go语言没有像gethostid()这样的功能,为什么不像其他平台无关的语言(如Java)一样实现它,可以在这里找到答案:
https://stackoverflow.com/questions/1986732/how-to-get-a-unique-computer-identifier-in-java-like-disk-id-or-motherboard-id

英文:

gethostid(3) is a UNIX/BSD specific libc function. reading from /etc/hostid would not work on non UNIX systems and is not platform independent.

since go does not provide something like gethostid() why not implement it like other platform independent languages like JAVA do, answered here:
https://stackoverflow.com/questions/1986732/how-to-get-a-unique-computer-identifier-in-java-like-disk-id-or-motherboard-id

答案3

得分: 0

使用此代码,您可以使用主机的UUID生成许可证。

package main

import (
	"encoding/base64"
	"fmt"
	"github.com/google/uuid"
)

func main() {

	uuid := uuid.New()
	uuidBytes := uuid[:]
	licenseKeyBytes := append(uuidBytes)
	licenseKey := base64.StdEncoding.EncodeToString(licenseKeyBytes)
	fmt.Println("生成的许可证密钥:", licenseKey)
}

请注意,此代码使用了github.com/google/uuid包和encoding/base64包。它生成一个UUID,并将其转换为字节切片。然后,将UUID字节切片附加到许可证密钥字节切片中,并使用base64编码将其转换为字符串。最后,打印生成的许可证密钥。

英文:

Using this code you can generate a license for host using uuid of host

package main

import (
        "encoding/base64"
        "fmt"
        "github.com/google/uuid"
)

func main() {

        uuid := uuid.New()
        uuidBytes := uuid[:]
        licenseKeyBytes := append(uuidBytes)
        licenseKey := base64.StdEncoding.EncodeToString(licenseKeyBytes)
        fmt.Println("Generated license key:", licenseKey)
}

huangapple
  • 本文由 发表于 2016年3月22日 15:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/36148433.html
匿名

发表评论

匿名网友

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

确定