Skip to content

Instantly share code, notes, and snippets.

@DominicWatts
Last active October 22, 2025 13:22
Show Gist options
  • Select an option

  • Save DominicWatts/3a806dd4f6878bda879f81448dd86af5 to your computer and use it in GitHub Desktop.

Select an option

Save DominicWatts/3a806dd4f6878bda879f81448dd86af5 to your computer and use it in GitHub Desktop.
Bash : top-ip-server-stats #bash

hits.sh

sh hits.sh > hits.txt

#!/bin/bash

cd /var/log/nginx || exit

for file in *access.log; do

    ### Ensure the log file exists
    if [[ ! -f "$file" ]]; then
        echo "Error: Log file $file not found!"
        exit 1
    fi

    echo "IP HIT REPORT"
    awk '{print $1}' "$file" | sort | uniq -c | sort -nr | head
    echo

    echo "TOP 10 IPs GEOIP INFO"
    for ip in $(awk '{print $1}' "$file" | sort | uniq -c | sort -nr | head | awk '{print $2}'); do
        echo "GEOIP Info for $ip:"
        curl -s ipinfo.io/$ip
        echo
    done

    echo

    ### Report by top 10 IPs
    for ip in $(awk '{print $1}' "$file" | sort | uniq -c | sort -nr | head | awk '{print $2}'); do
        echo "REFERRER REPORT BY TOP 10 IPs FOR IP $ip"
        grep "$ip" "$file" | awk -F\" '{print $4}' | sort | uniq -c | sort -nr | head
        echo

        echo "USER-AGENT REPORT BY TOP 10 IPs FOR IP $ip"
        grep "$ip" "$file" | awk -F\" '{print $6}' | sort | uniq -c | sort -nr | head
        echo
    done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment