Sunday, October 7, 2012

Java program to find all IP addresses of given Host

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Program to print all ip addresses of given domain
 * 
 * @author sahir maredia (Kotia Solutions)
 * 
 */
public class FindIP {

 /**
  * @param args
  */
 public static void main(String[] args) {
  if (args.length != 1) {
   System.out.println("Please provide a valid input");
   return;
  }
  try {
   InetAddress[] ipaddresses = InetAddress.getAllByName(args[0]);

   for (InetAddress ip : ipaddresses)
    System.out.println(ip.getHostAddress());
  } catch (UnknownHostException e) {
   System.out.println("Error occured : " + e.getMessage());
  }
 }

}

Run the program as follows:

sahir@sahir-laptop:~/networklab$ javac FindIP.java
sahir@sahir-laptop:~/networklab$ java FindIP google.com
74.125.236.163
74.125.236.164
74.125.236.165
74.125.236.166
74.125.236.167
74.125.236.168
74.125.236.169
74.125.236.174
74.125.236.160
74.125.236.161
74.125.236.162
2404:6800:4007:802:0:0:0:100e
sahir@sahir-laptop:~/networklab$

No comments:

Post a Comment