Skip to content

Instantly share code, notes, and snippets.

@sophieforceno
Last active January 10, 2022 23:16
Show Gist options
  • Save sophieforceno/509ae2fd2289d5406a2585b08d1a8512 to your computer and use it in GitHub Desktop.
Save sophieforceno/509ae2fd2289d5406a2585b08d1a8512 to your computer and use it in GitHub Desktop.
Returns daily or all forbidden or refused requests parsed from the Nginx error log
#! /bin/bash
case "$1" in
all)
sudo grep -E "forb|refuse" /var/log/nginx/error.log | awk '{ print $1 " " $2 " " $11 }' | grep -w '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | tr -d ','
# I also include a pipe to grep to remove all friendly IPs, something like:
# | grep -vE "192.168.1.*|$(curl -s ifconfig.co)|10.8.0.4"
;;
day)
sudo grep -E "forb|refuse" /var/log/nginx/error.log | awk '{ print $1 " " $2 " " $11 }' | grep -w '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' | tr -d ',' | grep "$(date +%Y/%m/%d)"
;;
*)
cat << EOF
nginxfail.sh
Usage: nginxfail.sh [OPTIONS] <file>
all Return all forbidden & refused requests
day Return forbidden & refused requests for today
EOF
;;
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment