英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论