如何从IP地址获取主机名?

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

how to get hostname from IP address?

问题

我正在尝试从IP地址获取主机名,但总是出现以下错误:

 错误:无法获取主机名,错误查找 220.220.103.65.in-addr.arpa.:没有这样的主机,IP地址为 65.103.220.220

代码如下:

var hostname string
hostnames, err := net.LookupAddr(c.ip)
 if err != nil {
  glog.Errorf("错误:无法获取主机名,错误 %v,IP地址 %v", err, c.ip)
  } else {
  hostname = strings.Join(hostnames, " ")
 }
英文:

I'm trying to get the hostname from Ip address but I always get error below :

 err couln't get hostnames,err lookup 220.220.103.65.in-addr.arpa.: no such host,ip 65.103.220.220

The code :

var hostname string
hostnames, err := net.LookupAddr(c.ip)
 if err != nil {
  glog.Errorf("err couln't get hostnames,err %v,ip %v", err, c.ip)
  } else {
  hostname = strings.Join(hostnames, " ")
 }

答案1

得分: 7

这不是一个错误,而是一个简单的陈述,即你正在寻找的IP地址没有PTR记录。并没有要求互联网上的每台机器都有PTR记录,并且尝试从IP地址查找名称并不保证成功。

简而言之,你的代码不会始终返回一个名称,你无法做任何事情来解决这个问题。

无论你使用机器名称做什么,你都不能依赖它。找到一种不同的方法。

英文:

That's not an error but a simple statement that the IP address you're looking for has no PTR record. There's no requirement that every machine on the Internet has a PTR record, and no guarantee that trying to find a name from the IP address will succeed.

In short, your code won't always return a name and there's nothing you can do about it.

Whatever it is you're using the machine name for, you can't rely on it. Find a different way.

huangapple
  • 本文由 发表于 2014年7月9日 18:02:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/24650689.html
匿名

发表评论

匿名网友

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

确定