如何找到本地主机的完全限定域名(FQDN)?

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

How do you find the FQDN of the local host?

问题

在Go语言中,你可以使用os.Hostname()函数来获取本地主机的完全限定域名(FQDN)。

顺便提一下,net.LookupAddr()在Windows上不起作用,所以这不是一个选择。

英文:

How do you find the FQDN of the local host in Go?

BTW: net.LookupAddr() doesn't work on Windows. So that's not an option.

答案1

得分: 3

默认情况下没有简短的方法。

os.Hostname() 默认情况下不提供完全限定域名(Fully Qualified Domain Name)。

cmd := exec.Command("/bin/hostname", "-f")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
    log.Errorf(err)
}
fqdn := out.String()
fqdn = fqdn[:len(fqdn)-1] // 移除行尾符
英文:

By default there is no short way.

os.Hostname() doesn't provide the Fully Qualified Domain Name by default.

cmd := exec.Command("/bin/hostname", "-f")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
	log.Errorf(err)
}
fqdn := out.String()
fqdn = fqdn[:len(fqdn)-1] // removing EOL

答案2

得分: 3

根据文档,函数os.Hostname()返回内核报告的系统主机名。所以,如果你的计算机名为computer1os.Hostname()返回computer1。如果你的计算机名为computer1.my.officeos.Hostname()返回computer1.my.office。在Windows上也是一样的。如果你想要获取域名(指Active Directory域),有四种方法:

  1. 解析以下命令的结果:wmic computersystem get domain
  2. 解析以下命令的结果:systeminfo | findstr /B /C:"Domain"
  3. 假设存在环境变量USERDNSDOMAIN并评估其值(注意:该变量的值是指用户所存储的域)
  4. 检查工作站分配的IP地址是否可以通过DNS解析(对于这一点,你可以查看这个链接:https://github.com/Showmax/go-fqdn)
英文:

According to the documentation, function os.Hostname() returns the system host name reported by kernel. So, if your computer is named computer1, os.Hostname() returns computer1. If your computer is named computer1.my.office, os.Hostname() returns computer1.my.office. On Windows, is the same. If you want the domain name (as referred to the Active Directory domain) you have four ways:

  1. Parse the result of this command: wmic computersystem get domain
  2. Parse the result of this command: systeminfo | findstr /B /C:"Domain"
  3. Assume the existence of the environment variable USERDNSDOMAIN and evaluate his value (note that: the value of this variable is referred at the domain which user is stored)
  4. Check if one of the ip's assigned to the workstation can be resolved via DNS (for this point, you can view this: https://github.com/Showmax/go-fqdn)

答案3

得分: 1

你可以使用net库执行一些体操,示例在这里

英文:

You can perform some gymnastics using the net lib as demonstrated here.

huangapple
  • 本文由 发表于 2015年10月3日 14:19:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/32919572.html
匿名

发表评论

匿名网友

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

确定