/******************************************************************************************
* Get the IP address of a remote server (If server has multiple IP addr get them all)
* Get the IP address of the local client
*
* Usage: getAddr <host>     (host can be either: hostname or IPaddress)
* 
* @version 1.0
* @author Sam Sultan
******************************************************************************************/
import java.net.*;

public class getAddr
{
    public static void main(String[] args)
    {
        if (args.length != 1) {
            System.out.println("Usage: getAddr hostname_or_IPaddress");
            System.exit(0);
        }

        try
        {
            InetAddress IPserver = InetAddress.getByName(args[0]);   //get server hostname -> address
        
            System.out.println("Remote toString()....: " + IPserver);
            System.out.println("Remote Server Name...: " + IPserver.getHostName());
            System.out.println("Remote Server IP Addr: " + IPserver.getHostAddress());

            System.out.println("Also known as...");

            InetAddress[] IPservers = InetAddress.getAllByName(args[0]);    //get All other IPs for that host

            for (int i=1; i < IPservers.length; i++)                        //skip the first
                System.out.println("   Remote toString().: " + IPservers[i]);


            System.out.println();

            InetAddress IPclient = InetAddress.getLocalHost();       //get local hostname -> address
//or        InetAddress IPclient = IPserver.getLocalHost();
  
            System.out.println("Local  toString()....: " + IPclient);
            System.out.println("Local  Client Name...: " + IPclient.getHostName());
            System.out.println("Local  Client IP Addr: " + IPclient.getHostAddress());
        }

        catch(UnknownHostException e) 
        {
            System.out.println("Unknown host: " + args[0]);
            System.exit(-1);
        }
    }
}