Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.message.logs;
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- import java.util.Arrays;
- import java.util.Stack;
- public class Main {
- public static void main(String[] args) {
- String fileLocation = null;
- boolean repeat = false;
- boolean isCommand = false;
- String arg;
- String command = null;
- String content = null;
- if(args.length == 0 && System.in == null) {
- help("Please enter a file with --file", true);
- }
- for (int i = 0; i < args.length; i++) {
- arg = args[i];
- if(isCommand) {
- isCommand = false;
- continue;
- }
- while(arg.substring(0,1).contains("-")) {
- arg = arg.substring(1);
- command = arg;
- isCommand = true;
- }
- if(!isCommand) continue;
- if(args.length <= i + 1) {
- isCommand = false;
- }
- else content = args[i + 1];
- // ADD ANY ARGUMENTS HERE
- if(command.contains("file")) {
- assert content != null;
- fileLocation = content;
- }
- else if(command.contains("repeat")) {
- repeat = true;
- isCommand = false;
- }
- else if(command.contains("help") || command.contains("h")) {
- help(true);
- }
- else {
- help("Unknown arguments : --" + command, true);
- }
- }
- if(fileLocation == null && System.in == null) {
- System.out.println("Please enter a file with --file");
- System.exit(1);
- }
- try
- {
- BufferedReader br;
- if(fileLocation != null) {
- br = new BufferedReader(new FileReader(fileLocation));
- }
- else {
- br = new BufferedReader((new InputStreamReader(System.in, StandardCharsets.UTF_8)));
- }
- String line;
- Stack<String> users = new Stack<>();
- int succeeded = 0;
- int failed;
- int total = 0;
- if(!br.ready()) {
- help("Cannot read the file", true);
- }
- while((line=br.readLine())!=null)
- {
- if(!line.contains("sshd[")) continue;
- String[] arr = Arrays.stream(line.split(" ")).filter(s -> !s.isEmpty()).toArray(String[]::new);
- if(arr.length < 11) continue;
- String success = arr[5];
- String failure = arr[8];
- String user = arr[10];
- if(!success.contains("Accepted")) {
- if(failure.contains("failure")) {
- total++;
- }
- continue;
- }
- total++;
- succeeded++;
- if(!repeat) {
- if (users.contains(user)) continue;
- users.add(user);
- }
- System.out.println((total + 1) + " " + user);
- }
- failed = total - succeeded;
- System.out.println();
- System.out.println("Total :");
- if(!repeat) System.out.println(users.size() + " unique IP SSH logins succeeded");
- System.out.println(succeeded + " SSH logins succeeded");
- System.out.println(failed + " SSH logins failed");
- System.out.println(total + " total SSH logins");
- System.exit(0);
- }
- catch(IOException e)
- {
- System.out.println("Cannot find this file");
- System.exit(1);
- }
- }
- static void help(boolean exit) {
- help("{{nothing}}", exit);
- }
- static void help(String additionalMessage, Boolean exit) {
- if(!additionalMessage.contains("{{nothing}}")) {
- System.out.println(additionalMessage);
- System.out.println();
- }
- System.out.println("Arguments :");
- System.out.println(" --file (required) : The document to analyse");
- System.out.println(" --repeat : Ignore if a IP has already been shown");
- System.out.println(" --help : Return help about the command");
- System.out.println();
- if(exit) System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement