Skip to content

Instantly share code, notes, and snippets.

@mypapit
Last active February 8, 2024 01:54
Show Gist options
  • Select an option

  • Save mypapit/c7fe9c335d7c2f605f11c591d0ff5f24 to your computer and use it in GitHub Desktop.

Select an option

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
#!/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