Viliam
Viliam

Reputation: 4414

How to test if the hostname given really exists?

How do you test if the given hostname really exists in Java?

The problem is with some DNS services like Opendns which will return IP address even for non-existent DNS entries and, therefore, InetAddress.getByName( host) will always return something.

However, the host command is able to detect it somehow:

~$ host owqieyuqowiery.com
owqieyuqowiery.com has address 67.215.77.132
Host owqieyuqowiery.com not found: 3(NXDOMAIN)

Upvotes: 3

Views: 5966

Answers (3)

Alnitak
Alnitak

Reputation: 339776

If you cannot trust the locally configured resolver (perhaps because it lies about domains that don't exist) the only alternative is to directly query the authoritative name server(s) for the domain in question.

For Java this should be possible using the dnsjava library.

You would need to start at the root name servers, and follow the referral chain down (in the same manner as a normal recursive server) to find the appropriate authoritative name server.

Upvotes: 1

jefflunt
jefflunt

Reputation: 33954

Depends on what you mean by "really exists." Do you mean that it's a registered domain, with a website behind it? A host name is just a more easily readable/memorable form vs. an IP address for a given system. In other words, I could have a domain registered (which is just a publicly accessible short name for a system somewhere), and not host a website on a server that said domain points to, and that doesn't make the domain any less "real".

if you just want to lookup A records, here's a solution:

If you want to know what host does, which might shed some light on why it does what it does, there's some information on it here: http://linux.die.net/man/1/host Basically when this fails, it means the DNS lookup failed. That is, the DNS server(s) that host connected to in order to lookup that domain returned zero results.

Also, host returns more than just the DNS A record (which is what is used for websites). It will also give you MX (mail server) records, etc.

Ex:

$ host google.com
google.com has address 74.125.225.48
google.com has address 74.125.225.49
google.com has address 74.125.225.50
google.com has address 74.125.225.51
google.com has address 74.125.225.52
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.

Upvotes: 4

James Jithin
James Jithin

Reputation: 10565

Use the InetAddress.getByName(address).isReachable(timeout); instead.

Upvotes: 3

Related Questions