英文:
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()返回内核报告的系统主机名。所以,如果你的计算机名为computer1,os.Hostname()返回computer1。如果你的计算机名为computer1.my.office,os.Hostname()返回computer1.my.office。在Windows上也是一样的。如果你想要获取域名(指Active Directory域),有四种方法:
- 解析以下命令的结果:
wmic computersystem get domain - 解析以下命令的结果:
systeminfo | findstr /B /C:"Domain" - 假设存在环境变量
USERDNSDOMAIN并评估其值(注意:该变量的值是指用户所存储的域) - 检查工作站分配的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:
- Parse the result of this command:
wmic computersystem get domain - Parse the result of this command:
systeminfo | findstr /B /C:"Domain" - Assume the existence of the environment variable
USERDNSDOMAINand evaluate his value (note that: the value of this variable is referred at the domain which user is stored) - 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论