Advertisement
Guest User

updated code

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