import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

public class JavaDnsLookup {

    public static void main(String[] args) {
        InetAddress ip;
        String hostname;

        try {
            ip = InetAddress.getLocalHost();
            hostname = ip.getHostName();
            System.out.println("Your Current IP address : " + ip.getHostAddress());
            System.out.println("Your Current Hostname : " + hostname + "\n");

            // Reverse DNS lookup
            System.out.println("Your Reverse DNS Hostname: " + ip.getCanonicalHostName() + "\n");
        } catch (UnknownHostException e) {
            System.out.println("Unable to determine local host information.");
            e.printStackTrace();
        }

        if (args.length > 0) {
            String myAddress = args[0];
            try {
                InetAddress[] arr = InetAddress.getAllByName(myAddress);
                System.out.println("The Hostname " + myAddress + " Resolves To: \n");
                for (InetAddress address : arr) {
                    System.out.println("IP Address: " + address.getHostAddress());
                    System.out.println("Canonical Hostname: " + address.getCanonicalHostName());
                    System.out.println("----");
                }
            } catch (UnknownHostException e) {
                System.out.println("Could not resolve the address: " + myAddress);
                e.printStackTrace();
            }
        } else {
            System.out.println("No address provided for lookup. Please pass a domain name as a parameter.");
        }
    }
}

