A Java program to print a DNS (Domain Name System) record for an Internet Address

Code

DNSLookup.java

// Print out DNS Record for an Internet Address
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class DNSLookup {
    public static void main(String args[]) {
        // explain what program does and how to use it
        if (args.length != 1) {
            System.err.println("Print out DNS Record for an Internet Address");
            System.err.println("USAGE: java DNSLookup domainName|domainAddress");
            System.exit(-1);
        }
        try {
            InetAddress inetAddress;
            // if first character is a digit then assume is an address
            if (Character.isDigit(args[0].charAt(0))) {
                // convert address from string representation to byte array
                byte[] b = new byte[4];
                String[] bytes = args[0].split("[.]");
                for (int i = 0; i < bytes.length; i++) {
                    b[i] = Integer.valueOf(bytes[i]).byteValue();
                }
                // get Internet Address of this host address
                inetAddress = InetAddress.getByAddress(b);
            }
            else {
                // get Internet Address of this host name
                inetAddress = InetAddress.getByName(args[0]);
            }
            // show the Internet Address as name/address
            System.out.println(inetAddress.getHostName() + "/" + inetAddress.getHostAddress());
            // get the default initial Directory Context
            InitialDirContext iDirC = new InitialDirContext();
            // get all the DNS records for inetAddress
            Attributes attributes =
                iDirC.getAttributes("dns:/" + inetAddress.getHostName(), new String[] {"*"});
            // get an enumeration of the attributes and print them out
            NamingEnumeration<? extends Attribute> attributeEnumeration = attributes.getAll();
            System.out.println("-- DNS INFORMATION --");
            while (attributeEnumeration.hasMore()) {
                System.out.println("" + attributeEnumeration.next());
            }
        }
        catch (UnknownHostException exception) {
            System.err.println("ERROR: No Internet Address for '" + args[0] + "'");
        }
        catch (NamingException exception) {
            System.err.println("ERROR: No DNS record for '" + args[0] + "'");
        }
    }
}

Sample Runs

c:\Users\admin\Documents\JAVA>java -version
java -version
java version "13.0.1" 2019-10-15
Java(TM) SE Runtime Environment (build 13.0.1+9)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)

c:\Users\admin\Documents\JAVA>javac -Xlint DNSLookup.java
javac -Xlint DNSLookup.java

c:\Users\admin\Documents\JAVA>java DNSLookup.java
java DNSLookup.java
Print out DNS Record for an Internet Address
USAGE: java DNSLookup domainName|domainAddress

c:\Users\admin\Documents\JAVA>java DNSLookup yahoo.com
java DNSLookup yahoo.com
yahoo.com/72.30.35.9
-- DNS INFORMATION --
AAAA: 2001:4998:58:1836::11, 2001:4998:58:1836::10, 2001:4998:44:41d::3, 2001:4998:44:41d::4, 2001:4998:c:1023::5, 2001:4998:c:1023::4
A: 72.30.35.10, 72.30.35.9, 98.137.246.7, 98.138.219.231, 98.137.246.8, 98.138.219.232
NS: ns1.yahoo.com., ns5.yahoo.com., ns4.yahoo.com., ns3.yahoo.com., ns2.yahoo.com.

c:\Users\admin\Documents\JAVA>java DNSLookup 72.30.35.9
java DNSLookup 72.30.35.9
media-router-fp1.prod1.media.vip.bf1.yahoo.com/72.30.35.9
-- DNS INFORMATION --
AAAA: 2001:4998:58:1836::10
A: 72.30.35.9

c:\Users\admin\Documents\JAVA>java DNSLookup 199.232.41
java DNSLookup 199.232.41
199.232.41.0/199.232.41.0
ERROR: No DNS record for '199.232.41'

c:\Users\admin\Documents\JAVA>java DNSLookup ghthytf.com
java DNSLookup ghthytf.com
ERROR: No Internet Address for 'ghthytf.com'

Copyright © 2019 Shayne Steele  
Comments / Suggestions / Corrections to : (shayne.steele <AT> eyeasme <DOT> com)
This document is licensed under the GFDLv1.3, All software shown is licensed under the LGPLv3.
Document last modified on December 11, 2019 07:22 pm EST.