Sunday, October 7, 2012

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$

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$

Java Program to find Computer name, IP Address and list all network interfaces

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Enumeration;

/**
 * Program to display current computer name, IP Address and network interfaces
 * 
 * @author sahir maredia (Kotia Solutions)
 * 
 */
public class ComputerName {

 /**
  * @param args
  */
 public static void main(String[] args) {
 try {

  InetAddress ipaddress = InetAddress.getLocalHost();
  System.out.println("Computer Host Name : "
    + ipaddress.getHostName());
  System.out.println("IP Address of Localhost : "
    + ipaddress.getHostAddress());

  // getting list of network interfaces
  Enumeration<NetworkInterface> interfaces = NetworkInterface
    .getNetworkInterfaces();

  System.out.println();
  System.out
    .println("================ Following are the available network interfaces ===============");
  System.out.println();
  if (interfaces == null) {
   System.out.println("No network interfaces found");
  } else {

   for (NetworkInterface netIf : Collections.list(interfaces)) {
    System.out.println("Display Name : "
      + netIf.getDisplayName());
    System.out.println("Name : " + netIf.getName());
    System.out.println();
   }
  }
 } catch (UnknownHostException ex) {
  System.out.println("Error Occured : " + ex.getMessage());
 } catch (SocketException e) {
  System.out.println("Error Occured : " + e.getMessage());
 }
 }

}

Run the program as follows :

sahir@sahir-laptop:~/temp/networklab$ javac ComputerName.java
sahir@sahir-laptop:~/temp/networklab$ java ComputerName
Computer Host Name : sahir-laptop
IP Address of Localhost : 127.0.1.1

================ Following are the available network interfaces ===============

Display Name : wlan0
Name : wlan0

Display Name : lo
Name : lo

sahir@sahir-laptop:~/temp/networklab$

Saturday, October 6, 2012

Add Custom Application to Ubuntu Dashboard

To add a custom application icon to Ubuntu dashboard, create a file with name AppName.desktop in ~/.local/share/applications Put Following into that file

[Desktop Entry]
Name=Application Name
Comment=Application Description
Exec=<path to executable;>
Icon=<path to application icon>
Terminal=false
Type=Application
StartupNotify=true
Keywords=<some keywords>