Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- class Lookup {
- //find_Ip_Info gets information about each of the domain names entered.
- //Inputs: args from command line (domain names)
- //Outputs: host name, canoical host name, reachable and ip address info
- public void find_Ip_Info(String[] args) throws Exception
- {
- int n = 0;
- int i = 0;
- List<String> ip_address = new ArrayList<String>();
- //Loop through all the arguments
- while (n < args.length)
- {
- InetAddress address;
- address = InetAddress.getByName(args[n]);
- System.out.println("--" + args[n] + "--");
- //Print infortmation about host names
- System.out.println("Host name: " + address.getHostAddress());
- System.out.println("Canoical Host name: " + address.getCanonicalHostName());
- System.out.println("Reachable?: " + address.isReachable(5000));
- String IpString = address.getHostAddress();
- //Print out type of IP address
- if (IpString.contains(".")) {
- System.out.println("IP Address Family: IPV4");
- ip_address.add(address.getHostAddress());
- }
- else if (IpString.contains(":")) {
- System.out.println("IP Adress Family: IPV6");
- }
- else {
- System.out.println("ERROR: IP Address Family not found.");
- }
- System.out.println();
- n = n + 1;
- }
- String[][] ipParts = new String[ip_address.size()][4];
- //Split IP address to find shared hierachy
- if(ip_address.size()>=2)
- {
- System.out.println("Comparing IP Addresses...");
- for(i=0; i<ip_address.size(); i++)
- {
- System.out.println(ip_address.get(i));
- String[] split = ip_address.get(i).split("\\.");
- for(int j=0; j<split.length; j++)
- {
- ipParts[i][j] = split[j];
- }
- }
- System.out.println();
- System.out.println("Max shared hierarchy:");
- boolean first = true;
- boolean second = true;
- boolean third = true;
- boolean fourth = true;
- for(i=0; i<ip_address.size(); i++)
- {
- for(int j=1; j<ip_address.size(); j++)
- {
- if(i==j) {
- break;
- }
- if(ipParts[i][0].equals(ipParts[j][0])) {
- }
- else
- {
- first = false;
- break;
- }
- if(ipParts[i][1].equals(ipParts[j][1]) && first == true)
- {
- }
- else
- {
- second = false;
- break;
- }
- if(ipParts[i][2].equals(ipParts[j][2]) && second == true) {
- }
- else
- {
- third = false;
- break;
- }
- if(ipParts[i][3].equals(ipParts[j][3]) && third == true)
- {
- }
- else {
- fourth = false;
- break;
- }
- }
- }
- //Printing max hierarchy
- if(!first)
- {
- System.out.println("*.*.*.*");
- }
- else if(!second)
- {
- System.out.println(ipParts[0][0]+".*.*.*");
- }
- else if(!third)
- {
- System.out.println(ipParts[0][0]+"."+ipParts[0][1]+".*.*");
- }
- else if(!fourth)
- {
- System.out.println(ipParts[0][0]+"."+ipParts[0][1]+"."+ipParts[0][2]+".*");
- }
- else if(fourth)
- {
- System.out.println(ipParts[0][0]+"."+ipParts[0][1]+"."+ipParts[0][2]+"."+ipParts[0][3]);
- }
- }
- }
- }
- public class cwk1 {
- //check if any arguments are entered at CL.
- public static void main(String[] args) throws Exception {
- if(args.length==0)
- {
- System.out.println();
- KeyboardScanner kbd = new KeyboardScanner();
- kbd.continuousScan();
- }
- Lookup lookup = new Lookup();
- System.out.println();
- lookup.find_Ip_Info(args);
- }
- }
- //Keyboard scanner allows for domain names to be entered at runtime
- class KeyboardScanner
- {
- // java.util.Scanner
- private Scanner kbdReader = null;
- // Constructor; initialises the Scanner to stdin
- public KeyboardScanner()
- {
- kbdReader = new Scanner( System.in );
- }
- // Infinite loop that scans the keyboard and outputs (Ctrl-C to break).
- public void continuousScan() throws Exception
- {
- System.out.println( "In continuous scan mode: Start typing" );
- while( true )
- {
- // Read a line from stdin
- String cmd = kbdReader.nextLine();
- System.out.println( "LINE: '" + cmd + "'" );
- // Split into segments separated by spaces.
- String[] segmented = cmd.split(" ");
- System.out.print( "SEGMENTED:" );
- for( String segment : segmented ) System.out.print( " '" + segment + "'" );
- System.out.println();
- System.out.println();
- Lookup lookup = new Lookup();
- lookup.find_Ip_Info(segmented);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement