How do you find the FQDN of the local host?

依然范特西╮ 提交于 2020-07-18 07:58:06

问题


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:


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:


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:


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



来源:https://stackoverflow.com/questions/32919572/how-do-you-find-the-fqdn-of-the-local-host

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!