如何从IP解析DNS名称? [英] How to resolve the DNS name from IP?

查看:67
本文介绍了如何从IP解析DNS名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从客户端IP地址解析域名.为了进行测试,我使用了stackoverflow,如下所示.但是当我使用ip解析域时,仍然只能得到IP.

I want to resolve the domain name from a clients IP address. For a test, I used stackoverflow as follows. But when I use the ip to resolve the domain, I still get only the IP as result.

    String ip = InetAddress.getByName("www.stackoverflow.com").getHostAddress();
    System.out.println(ip);
    System.out.println(InetAddress.getByName(ip).getHostName());
    System.out.println(InetAddress.getByName(ip).getCanonicalHostName());
    System.out.println(InetAddress.getByName(ip).getHostAddress());

结果:

151.101.129.69
151.101.129.69
151.101.129.69
151.101.129.69

如果我只有ip ,如何获取域名(stackoverflow/ www.stackoverflow.com ).151.101.129.69 已给出?

How can I get the domain name (stackoverflow / www.stackoverflow.com) if I'd only have the ip 151.101.129.69 given?

推荐答案

我尝试在"www.stackoverflow.com"上进行正向查找,然后反向查找很明显,stackoverflow的管理员尚未为此IP设置DNS PTR记录,这将允许Java反向查找站点的DNS名称.

I tried a forward and then reverse lookup on "www.stackoverflow.com" and it is clear that the admins for stackoverflow have not set up the DNS PTR record(s) for this IP that would allow Java to reverse lookup the site's DNS name(s).

(反向查找将失败的另一种情况是没有DNS名称映射到给定IP地址时.)

(Another scenario where reverse lookup will fail is when no DNS name maps to a given IP address.)

这不是Java特定的问题.您将以任何语言获得相同的行为.这实际上是DNS设计的结果,也是DNS在实践中使用的方式.

This is not a Java specific problem. You will get the same behavior in any language. It is actually a consequence of DNS's design, and the way that DNS is used in practice.

对此没有真正的解决方案.您需要将您的应用程序代码写入 allow ,以获取无法解析为DNS地址的IP地址.

There is no real solution to this. You need to write your application code to allow for IP addresses that you cannot resolve to a DNS address.

记录下来,这就是我使用 dig 工具得到的.(出于安全原因,一些细节被遮盖了.)

For the record, this is what I got using the dig tool. (Some details obscured for security reasons).

$ dig www.stackoverflow.com

; <<>> DiG 9.11.3-1ubuntu1.13-Ubuntu <<>> www.stackoverflow.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52578
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: zzzzzz (good)
;; QUESTION SECTION:
;www.stackoverflow.com.     IN  A

;; ANSWER SECTION:
www.stackoverflow.com.  3600    IN  CNAME   stackoverflow.com.
stackoverflow.com.  2760    IN  A   151.101.129.69
stackoverflow.com.  2760    IN  A   151.101.193.69
stackoverflow.com.  2760    IN  A   151.101.1.69
stackoverflow.com.  2760    IN  A   151.101.65.69

;; Query time: 22 msec
;; SERVER: xx.xx.xx.xx
;; WHEN: Wed Oct 07 19:49:12 AEDT 2020
;; MSG SIZE  rcvd: 156

$ dig -x 151.101.129.69

; <<>> DiG 9.11.3-1ubuntu1.13-Ubuntu <<>> -x 151.101.129.69
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 43513
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: zzzzzz (good)
;; QUESTION SECTION:
;69.129.101.151.in-addr.arpa.   IN  PTR

;; AUTHORITY SECTION:
151.in-addr.arpa.   3600    IN  SOA pri.authdns.ripe.net. dns.ripe.net. 1586416382 3600 600 864000 3600

;; Query time: 34 msec
;; SERVER: xx.xx.xx.xx
;; WHEN: Wed Oct 07 19:49:26 AEDT 2020
;; MSG SIZE  rcvd: 168

这篇关于如何从IP解析DNS名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆