Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. import java.net.*;
  2.  
  3. import java.io.*;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.List;
  8.  
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.util.*;
  12.  
  13. class Lookup {
  14.  
  15. //find_Ip_Info gets information about each of the domain names entered.
  16. //Inputs: args from command line (domain names)
  17. //Outputs: host name, canoical host name, reachable and ip address info
  18. public void find_Ip_Info(String[] args) throws Exception
  19. {
  20. int n = 0;
  21. int i = 0;
  22. List<String> ip_address = new ArrayList<String>();
  23. //Loop through all the arguments
  24. while (n < args.length)
  25. {
  26. InetAddress address;
  27. address = InetAddress.getByName(args[n]);
  28. System.out.println("--" + args[n] + "--");
  29. //Print infortmation about host names
  30. System.out.println("Host name: " + address.getHostAddress());
  31. System.out.println("Canoical Host name: " + address.getCanonicalHostName());
  32. System.out.println("Reachable?: " + address.isReachable(5000));
  33.  
  34. String IpString = address.getHostAddress();
  35.  
  36. //Print out type of IP address
  37. if (IpString.contains(".")) {
  38. System.out.println("IP Address Family: IPV4");
  39. ip_address.add(address.getHostAddress());
  40. }
  41. else if (IpString.contains(":")) {
  42. System.out.println("IP Adress Family: IPV6");
  43. }
  44. else {
  45. System.out.println("ERROR: IP Address Family not found.");
  46. }
  47. System.out.println();
  48. n = n + 1;
  49. }
  50.  
  51. String[][] ipParts = new String[ip_address.size()][4];
  52.  
  53.  
  54. //Split IP address to find shared hierachy
  55. if(ip_address.size()>=2)
  56. {
  57. System.out.println("Comparing IP Addresses...");
  58.  
  59. for(i=0; i<ip_address.size(); i++)
  60. {
  61. System.out.println(ip_address.get(i));
  62. String[] split = ip_address.get(i).split("\\.");
  63. for(int j=0; j<split.length; j++)
  64. {
  65. ipParts[i][j] = split[j];
  66. }
  67.  
  68.  
  69. }
  70. System.out.println();
  71. System.out.println("Max shared hierarchy:");
  72. boolean first = true;
  73. boolean second = true;
  74. boolean third = true;
  75. boolean fourth = true;
  76.  
  77.  
  78. for(i=0; i<ip_address.size(); i++)
  79. {
  80. for(int j=1; j<ip_address.size(); j++)
  81. {
  82. if(i==j) {
  83. break;
  84. }
  85. if(ipParts[i][0].equals(ipParts[j][0])) {
  86.  
  87. }
  88. else
  89. {
  90. first = false;
  91. break;
  92. }
  93. if(ipParts[i][1].equals(ipParts[j][1]) && first == true)
  94. {
  95.  
  96. }
  97. else
  98. {
  99. second = false;
  100. break;
  101. }
  102. if(ipParts[i][2].equals(ipParts[j][2]) && second == true) {
  103.  
  104. }
  105. else
  106. {
  107. third = false;
  108. break;
  109. }
  110. if(ipParts[i][3].equals(ipParts[j][3]) && third == true)
  111. {
  112.  
  113. }
  114. else {
  115. fourth = false;
  116. break;
  117. }
  118. }
  119. }
  120.  
  121. //Printing max hierarchy
  122. if(!first)
  123. {
  124. System.out.println("*.*.*.*");
  125. }
  126. else if(!second)
  127. {
  128. System.out.println(ipParts[0][0]+".*.*.*");
  129. }
  130. else if(!third)
  131. {
  132. System.out.println(ipParts[0][0]+"."+ipParts[0][1]+".*.*");
  133. }
  134. else if(!fourth)
  135. {
  136. System.out.println(ipParts[0][0]+"."+ipParts[0][1]+"."+ipParts[0][2]+".*");
  137. }
  138.  
  139. else if(fourth)
  140. {
  141. System.out.println(ipParts[0][0]+"."+ipParts[0][1]+"."+ipParts[0][2]+"."+ipParts[0][3]);
  142. }
  143.  
  144. }
  145. }
  146. }
  147. public class cwk1 {
  148.  
  149. //check if any arguments are entered at CL.
  150. public static void main(String[] args) throws Exception {
  151. if(args.length==0)
  152. {
  153. System.out.println();
  154. KeyboardScanner kbd = new KeyboardScanner();
  155. kbd.continuousScan();
  156. }
  157. Lookup lookup = new Lookup();
  158. System.out.println();
  159. lookup.find_Ip_Info(args);
  160. }
  161. }
  162.  
  163.  
  164. //Keyboard scanner allows for domain names to be entered at runtime
  165. class KeyboardScanner
  166. {
  167.  
  168. // java.util.Scanner
  169. private Scanner kbdReader = null;
  170.  
  171. // Constructor; initialises the Scanner to stdin
  172. public KeyboardScanner()
  173. {
  174. kbdReader = new Scanner( System.in );
  175. }
  176.  
  177. // Infinite loop that scans the keyboard and outputs (Ctrl-C to break).
  178. public void continuousScan() throws Exception
  179. {
  180. System.out.println( "In continuous scan mode: Start typing" );
  181.  
  182. while( true )
  183. {
  184.  
  185. // Read a line from stdin
  186. String cmd = kbdReader.nextLine();
  187. System.out.println( "LINE: '" + cmd + "'" );
  188.  
  189. // Split into segments separated by spaces.
  190. String[] segmented = cmd.split(" ");
  191. System.out.print( "SEGMENTED:" );
  192. for( String segment : segmented ) System.out.print( " '" + segment + "'" );
  193. System.out.println();
  194. System.out.println();
  195. Lookup lookup = new Lookup();
  196. lookup.find_Ip_Info(segmented);
  197. }
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement