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