Advertisement
Guest User

the code

a guest
May 16th, 2022
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. package com.message.logs;
  2.  
  3. import java.io.*;
  4. import java.util.Stack;
  5.  
  6. public class Main {
  7. public static void main(String[] args) {
  8.  
  9. String fileLocation = null;
  10. boolean repeat = false;
  11. boolean isCommand = false;
  12. String arg;
  13. String command = null;
  14. String content = null;
  15. if(args.length == 0 && System.in == null) {
  16. help("Please enter a file with --file", true);
  17. }
  18. for (int i = 0; i < args.length; i++) {
  19. arg = args[i];
  20. if(isCommand) {
  21. isCommand = false;
  22. continue;
  23. }
  24. while(arg.substring(0,1).contains("-")) {
  25. arg = arg.substring(1);
  26. command = arg;
  27. isCommand = true;
  28. }
  29.  
  30. if(!isCommand) continue;
  31.  
  32. if(args.length <= i + 1) {
  33. isCommand = false;
  34. }
  35. else content = args[i + 1];
  36.  
  37. // ADD ANY ARGUMENTS HERE
  38. if(command.contains("file")) {
  39. fileLocation = "./" + content;
  40. }
  41. else if(command.contains("repeat")) {
  42. repeat = true;
  43. isCommand = false;
  44. }
  45. else if(command.contains("help") || command.contains("h")) {
  46. help(true);
  47. }
  48. else {
  49. help("Unknown arguments : --" + command, true);
  50. }
  51. }
  52. if(fileLocation == null && System.in == null) {
  53. System.out.println("Please enter a file with --file");
  54. System.exit(1);
  55. }
  56.  
  57. try
  58. {
  59. BufferedReader br;
  60. if(fileLocation != null) {
  61. br = new BufferedReader(new FileReader(fileLocation));
  62. }
  63. else {
  64. br = new BufferedReader((new InputStreamReader(System.in, "UTF-8")));
  65. }
  66. String line;
  67. Stack<String> users = new Stack<>();
  68. int succeeded = 0;
  69. int failed;
  70. int total = 0;
  71.  
  72. if(!br.ready()) {
  73. help("Cannot read the file", true);
  74. }
  75. while((line=br.readLine())!=null)
  76. {
  77. if(!line.contains("sshd")) continue;
  78. String[] arr = line.split("\\s+");
  79. if(arr.length < 11) continue;
  80.  
  81.  
  82. String log = arr[4];
  83. String log2 = arr[5];
  84. String log3 = arr[8];
  85. String user = arr[10];
  86. if(!log.contains("sshd")) continue;
  87. if(!log2.contains("Accepted")) {
  88. if(log3.contains("failure")) {
  89. total++;
  90. }
  91. continue;
  92. }
  93. total++;
  94. succeeded++;
  95.  
  96. if(!repeat) {
  97. if (users.contains(user)) continue;
  98. users.add(user);
  99. }
  100.  
  101. System.out.println((total + 1) + " " + user);
  102. }
  103.  
  104. failed = total - succeeded;
  105. System.out.println();
  106. System.out.println("Total :");
  107. if(!repeat) System.out.println(users.size() + " unique IP SSH logins succeeded");
  108. System.out.println(succeeded + " SSH logins succeeded");
  109. System.out.println(failed + " SSH logins failed");
  110. System.out.println(total + " total SSH logins");
  111. System.exit(0);
  112. }
  113. catch(IOException e)
  114. {
  115. System.out.println("Cannot find this file");
  116. System.exit(1);
  117. }
  118. }
  119.  
  120. static void help(boolean exit) {
  121. help("{{nothing}}", exit);
  122. }
  123.  
  124. static void help(String additionalMessage, Boolean exit) {
  125. if(!additionalMessage.contains("{{nothing}}")) {
  126. System.out.println(additionalMessage);
  127. System.out.println();
  128. }
  129. System.out.println("Arguments :");
  130. System.out.println(" --file (required) : The document to analyse");
  131. System.out.println(" --repeat : Ignore if a IP has already been shown");
  132. System.out.println(" --help : Return help about the command");
  133. System.out.println();
  134. if(exit) System.exit(0);
  135. }
  136. }
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement