GoLang: net.LookupHost 返回重复的 IP 地址

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

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

huangapple
  • 本文由 发表于 2023年3月26日 11:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75845664.html
匿名

发表评论

匿名网友

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

确定