Sunday, October 7, 2012

Java program to save a given URL into a file

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Program to save web page in a file
 * 
 * @author sahir maredia (Kotia Solutions)
 * 
 */
public class SaveWebPage {

 /**
  * @param args
  */
 public static void main(String[] args) {
  if (args.length != 2) {
   System.out.println("Please provide valid arguments");
   return;
  }

  try {
   URL url = new URL(args[0]);
   BufferedReader br = new BufferedReader(new InputStreamReader(
     url.openStream()));
   BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));

   String input = "";
   while ((input = br.readLine()) != null) {
    bw.write(input);
    bw.newLine();
   }

   bw.close();
   br.close();
   System.out.println("Web page written successfully.");
  } catch (MalformedURLException e) {
   System.out.println("Error occured : " + e.getMessage());
  } catch (IOException e) {
   System.out.println("Error occured : " + e.getMessage());
  }
 }

}

Run this program as follows :

sahir@sahir-laptop:~/networklab$ javac SaveWebPage.java
sahir@sahir-laptop:~/networklab$ java SaveWebPage http://www.kotiasolutions.com/services.html services.txt
Web page written successfully.
sahir@sahir-laptop:~/networklab$

Java program to print parts of given URL

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Program to print all the parts of the given url
 * 
 * @author sahir maredia (Kotia Solutions)
 * 
 */
public class URLParts {

 /**
  * @param args
  */
 public static void main(String[] args) {
  if (args.length != 1) {
   System.out.println("Please provide valid arguments");
   return;
  }
  try {
   URL url = new URL(args[0]);
   System.out.println("====== Given URL is : " + args[0] + " ======");
   System.out.println("Protocol : " + url.getProtocol());

   // if port not specified as part of url, getting default port
   int port = url.getPort() == -1 ? url.getDefaultPort() : url
     .getPort();
   System.out.println("Port : "
 + port);
   System.out.println("Host : " + url.getHost());
   System.out.println("Path : " + url.getPath());
   System.out.println("File Name : " + url.getFile());
   System.out.println("Query : " + url.getQuery());
  } catch (MalformedURLException e) {
   System.out.println(e.getMessage());
  }

 }

}

Run this program as follows :

sahir@sahir-laptop:~/networklab$ javac URLParts.java
sahir@sahir-laptop:~/networklab$ java URLParts http://www.kotiasolutions.com/services.html?service=web
====== Given URL is : http://www.kotiasolutions.com/services.html?service=web ======
Protocol : http
Port : 80
Host : www.kotiasolutions.com
Path : /services.html
File Name : /services.html?service=web
Query : service=web
sahir@sahir-laptop:~/networklab$


Simple nslookup java clone

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

/**
 * Program to perform simple functionality of NSLookUp (simple nslookup clone)
 * 
 * @author sahir maredia (Kotia Solutions)
 * 
 */
public class NSLookup {

 /**
  * @param args
  */
 public static void main(String[] args) {
  if (args.length != 1) {
   System.out.println("Please provide valid arguments");
   return;
  }

  try {
   InetAddress ipaddress = InetAddress.getByName(args[0]);
   System.out.println("Name : " + ipaddress.getHostName());
   System.out.println("Address : " + ipaddress.getHostAddress());
  } catch (UnknownHostException e) {
   System.out.println("Error occured : " + e.getMessage());
  }
 }

}

Run this program as follows:

sahir@sahir-laptop:~/networklab$ javac NSLookup.java
sahir@sahir-laptop:~/networklab$ java NSLookup google.com
Name : google.com
Address : 74.125.236.163
sahir@sahir-laptop:~/networklab$ java NSLookup 74.125.236.163
Name : maa03s16-in-f3.1e100.net
Address : 74.125.236.163
sahir@sahir-laptop:~/networklab$

Java program to find the whether given IP is IPv4 or IPv6

import java.util.regex.Pattern;

/**
 * Program to find whether given ip is IPv4 or IPv6 address
 * 
 * @author sahir maredia (Kotia Solutions)
 * 
 */
public class IPType {

 private static final String IPv4 = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
 private static final String IPv6 = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}";

 /**
  * @param args
  */
 public static void main(String[] args) {
  if (args.length != 1) {
   System.out.println("Please provide IP address as argument");
   return;
  }
  String ipaddress = args[0];
  
  Pattern ipv4 = Pattern.compile(IPv4);
  Pattern ipv6 = Pattern.compile(IPv6);
  if (ipv4.matcher(ipaddress).matches()) {
   System.out.println("Given IP Address : " + ipaddress + " is IPv4");
   return;
  } else if (ipv6.matcher(ipaddress).matches()) {
   System.out.println("Given IP Address : " + ipaddress + " is IPv6");
   return;
  } else {
   System.out.println("Given string is an invalid IP address");
  }
 }

}

Run this program as follows :

sahir@sahir-laptop:~/networklab$ javac IPType.java
sahir@sahir-laptop:~/networklab$ java IPType 192.168.1.1
Given IP Address : 192.168.1.1 is IPv4
sahir@sahir-laptop:~/networklab$ java IPType 0000:ffff:1234:4567:0000:4321:4567:ffff
Given IP Address : 0000:ffff:1234:4567:0000:4321:4567:ffff is IPv6
sahir@sahir-laptop:~/networklab$