How to Obtain the IP Address and DNS Details for a domain in java


In the following snippet you can find out how to use the InetAddress class to obtain the ip of the server where a domain is hosted along with the DNS information. Using this class you’ll obtain only the technical DNS details(name server, SOA & MX records), not the emails or the names of the domain owners.

	static public void printDomainDetails(String domain)
			throws UnknownHostException, NamingException
	{
		InetAddress netAddress = InetAddress.getByName(domain);
		String ipAddress = netAddress.getHostAddress();
		
		System.out.println(domain + " / " + ipAddress);
		
		InitialDirContext initialDirContext = new InitialDirContext();

		// get the DNS records:
		Attributes attributes = initialDirContext.getAttributes(
												"dns:/" + domain);

		// get an enumeration of the attributes:
		NamingEnumeration<? extends Attribute> attributeEnumeration
												 = attributes.getAll();

		System.out.println("DNS Details:");
		while (attributeEnumeration.hasMore())
		{
			Attribute attribute = attributeEnumeration.next();
			System.out.println(attribute.toString());
		}
		attributeEnumeration.close();	
	}

Leave a Reply

Your email address will not be published. Required fields are marked *