-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogAnalyzer.java
More file actions
57 lines (41 loc) · 1.79 KB
/
LogAnalyzer.java
File metadata and controls
57 lines (41 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
public class LogAnalyzer {
public static void main(String[] args) {
HashMap<String, Integer> ipFailures = new HashMap<>();
int blockThreshold = 5;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
try {
File file = new File("logins.txt");
Scanner reader = new Scanner(file);
while (reader.hasNextLine()) {
String line = reader.nextLine();
String[] parts = line.split(",");
LocalDateTime time = LocalDateTime.parse(parts[0], formatter);
String ip = parts[1];
String username = parts[2];
String password = parts[3];
if (!(username.equals("admin") && password.equals("1234"))) {
ipFailures.put(ip, ipFailures.getOrDefault(ip, 0) + 1);
}
}
reader.close();
FileWriter blockWriter = new FileWriter("blocked_ips.txt");
System.out.println("=== Firewall Auto-Block Simulation ===");
for (String ip : ipFailures.keySet()) {
int count = ipFailures.get(ip);
System.out.println(ip + " → " + count + " failed attempts");
if (count >= blockThreshold) {
System.out.println("🚫 BLOCKED: " + ip + " exceeded failed login threshold.");
blockWriter.write(ip + "\n");
}
}
blockWriter.close();
System.out.println("\nBlocked IP list saved to blocked_ips.txt");
} catch (Exception e) {
System.out.println("Error processing logs.");
}
}
}