Last active
February 8, 2024 01:54
-
-
Save mypapit/c7fe9c335d7c2f605f11c591d0ff5f24 to your computer and use it in GitHub Desktop.
Analyze the log file to find the IP that accessed the webserver server the most
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Check if a log file was specified | |
| if [ "$#" -ne 1 ]; then | |
| echo "Usage: $0 access.log" | |
| exit 1 | |
| fi | |
| LOG_FILE="$1" | |
| if [ ! -f "$LOG_FILE" ]; then | |
| echo "Error: Log file does not exist." | |
| exit 1 | |
| fi | |
| TOP_IPS=10 | |
| # Analyze the log file to find the top IP addresses | |
| echo "Analyzing the log file to find the top $TOP_IPS IP addresses..." | |
| awk '{print $1}' "$LOG_FILE" | sort | uniq -c | sort -nr | head -n "$TOP_IPS" | awk '{print "IP: " $2 ", Access Count: " $1}' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment