首页 文章

nslookup和Java的InetAddress getHostName之间的结果不同

提问于
浏览
3

我正在尝试编写一个简单的java程序,它将使用以下代码返回我执行的ip地址的dns名称:

InetAddress host = InetAddress.getByName(ip);
String dnsName = host.getHostName();

当注册了dns名称时,getHostName()返回此名称,当没有现有的dns名称时,返回ip地址 .

对于许多地址,当nslookup命令返回时,上面的代码不会返回任何地址 .

例如,对于地址82.117.193.169,nslookup返回 peer-AS31042.sbb.rs ,而getHostName()仅返回地址 . 对于所有地址都不会发生这种情况,但是对于大量情况 .

2 回答

  • 1

    您的计算机可能未默认配置为使用DNS,即使它是按需提供的 .

    我会尝试

    ping 82.117.193.169
    

    并查看它是否将IP地址解析为主机名 .

  • 1

    这是由于“A”记录检查 - Java希望你正在查找的IP号码被列在那里 . 他们称之为“XXX”,我明白为什么:

    private static String getHostFromNameService(InetAddress addr, boolean check) {
    String host = null;
    for (NameService nameService : nameServices) {
        try {
            // first lookup the hostname
            host = nameService.getHostByAddr(addr.getAddress());
    
            /* check to see if calling code is allowed to know
             * the hostname for this IP address, ie, connect to the host
             */
            if (check) {
                SecurityManager sec = System.getSecurityManager();
                if (sec != null) {
                    sec.checkConnect(host, -1);
                }
            }
    
            /* now get all the IP addresses for this hostname,
             * and make sure one of them matches the original IP
             * address. We do this to try and prevent spoofing.
             */
    
            InetAddress[] arr = InetAddress.getAllByName0(host, check);
            boolean ok = false;
    
            if(arr != null) {
                for(int i = 0; !ok && i < arr.length; i++) {
                    ok = addr.equals(arr[i]);
                }
            }
    
            //XXX: if it looks a spoof just return the address?
            if (!ok) {
                host = addr.getHostAddress();
                return host;
            }
    
            break;
    

相关问题