英文:
GoLang: net.LookupHost returns duplicate ips
问题
我正在尝试运行net.LookupHost来获取运行我的代码的机器的IP地址。它在大多数机器上都能正常工作,但在少数机器上,它返回一个包含两个条目的切片:正确的IP和一个重复的相同IP的条目。
我尝试在主机上运行nslookup,它只返回一个条目。
将代码添加到问题中:
hostname, _ := os.Hostname()
ips, err := net.LookupHost(hostname)
if err != nil {
fmt.Printf("%d\n", err.Error())
}
fmt.Printf("Hello, 世界 %#v \n", ips)
ips2, err := net.LookupIP(hostname)
if err != nil {
fmt.Printf("%d\n", err.Error())
}
fmt.Printf("Hello, 世界 %#v \n", ips2)
我期望net.LookupHost只在响应中给出一个条目,但它在响应中给出了两个相同的条目。
英文:
I'm trying to run net.LookupHost to get ip address of the machine on which my code is running. It is working on most of the machines but on few machines, it is returning a slice with two entries: Correct ip and a duplicate entry with the same IP.
I tried running nslookup on the host and it is only returning one entry.
Adding the code to the question -
hostname, _ := os.Hostname()
ips, err := net.LookupHost(hostname)
if err != nil {
fmt.Printf("%d\n", err.Error())
}
fmt.Printf("Hello, 世界 %#v \n", ips)
ips2, err := net.LookupIP(hostname)
if err != nil {
fmt.Printf("%d\n", err.Error())
}
fmt.Printf("Hello, 世界 %#v \n", ips2)
I expected net.LookupHost to only give one entry in the response but it is giving two identical entries in the response.
答案1
得分: 2
看起来是一个DNS解析问题。
如果在其他服务器上可以工作,那么尝试检查文件/etc/hosts
。
系统首先查阅
/etc/hosts
文件,然后再查阅DNS服务器。
来源:https://debian-handbook.info/browse/stable/sect.hostname-name-service.html
英文:
Looks like a DNS resolution issue.
If it is working from other servers then try checking out the file /etc/hosts
.
> The system consults the /etc/hosts file first, then DNS servers.
source: https://debian-handbook.info/browse/stable/sect.hostname-name-service.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论