简体   繁体   中英

Inetaddress returning unexpected Hostname

I have the below code which i use to get the full canonical hostname of a server before i move forward. This is returning me a value( partuicularly different domain) on my server. The nslookup on the command line returns the correct value.

I am trying to understand what exactly java.inet does internally to resolve the correct full canonical name. Does it queries the DNS servers?

import java.net.InetAddress;

public class IpLookup {

    public static void main(String[] args) {

        try{

            String REQUESTSERVER = args[0];  
            InetAddress in = InetAddress.getByName(REQUESTSERVER);
            REQUESTSERVER = in.getCanonicalHostName();
            System.out.println("Canonical REQUESTSERVER "+ REQUESTSERVER );
        } catch(Exception e) {
            System.out.println("lookup failed");
        }
    }
}

What is happening is that nslookup is returning the hostname you entered along with its IP address(es), while the Java code is first looking up the IP and then using reverse DNS based on the returned IP address. rDNS is probably not configured to return what you think of as the "canonical" address.

I ran nslookup on www.google.com :

> www.google.com
Non-authoritative answer:
Server:  perseus.jhmg.pvt
Address:  192.168.10.254

Name:    www.google.com
Addresses:  2607:f8b0:400a:801::1010
          173.194.33.146
          173.194.33.144
          173.194.33.148
          173.194.33.147
          173.194.33.145

Then I ran your program on www.google.com and got this result:

Canonical REQUESTSERVER sea09s17-in-f17.1e100.net

Doing a reverse DNS search on the first address returned by nslookup gives:

> 173.194.33.146
Server:  perseus.jhmg.pvt
Address:  192.168.10.254

Name:    sea09s17-in-f18.1e100.net
Address:  173.194.33.146

(BTW, note 1e100 is 10 100 or the number known as a "googol"... cute :-)

Which proves that's what is happening.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM