Sunday, June 26, 2011

Find IP Address of a machine using Java (java.net)

Code to Find the IP Address of a local machine or any website on the network

import java.io.IOException;

import java.net.*;
public class Network {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        // Get Hostname of the local machine
        sop("Local Host Name"+InetAddress.getLocalHost());
        //Get IP of google.co.in
        //Get IP of google.com
        sop("IP of website google.co.in : "+InetAddress.getByName("google.co.in"));
        sop("IP of website google.com  : "+InetAddress.getByName("google.com"));
        sop("IP address of my website "+InetAddress.getByName("javaapionline.com"));
       
   
       // Google has multiple IPs. To List all the IP's use getAllByName
        InetAddress SW[] = InetAddress.getAllByName("www.google.com");
        for (int i=0; i
        System.out.println(SW[i]);
       
       
        // Google - Sites - Using Instance of Address
        // Access hostname or host address
        InetAddress Address = InetAddress.getByName("google.com");
        sop("Google Host Name : "+Address.getHostName());
        sop("Google Host Address : "+Address.getHostAddress());
             
    }

    public static void sop(String b)
    {
        System.out.println(b);
       
    }
    public static void sop(boolean b)
    {
        System.out.println(b);
    }
}


--------------------------------------------------------------------------------

Sample Output ( Could Vary on your machine )
Local Host Name : xxx-xxx/124.123.161.188
IP of website google.co.in : google.co.in/74.125.236.49
IP of website google.com  : google.com/74.125.236.51
IP address of my website javaapionline.com/38.101.213.236
www.google.com/74.125.236.48
www.google.com/74.125.236.49
www.google.com/74.125.236.50
www.google.com/74.125.236.52
www.google.com/74.125.236.51
Google Host Name : google.com
Google Host Address74.125.236.51

No comments:

Post a Comment

Please add value. Sharing is caring