Skip to content

Instantly share code, notes, and snippets.

@jcolson
Created September 17, 2019 14:59
Show Gist options
  • Save jcolson/c70ba77095aa108c0efaeae780b5ed93 to your computer and use it in GitHub Desktop.
Save jcolson/c70ba77095aa108c0efaeae780b5ed93 to your computer and use it in GitHub Desktop.
#!/bin/sh
number=200
CMDS="curl jq"
for i in $CMDS
do
# command -v will return >0 when the $i is not found
command -v $i >/dev/null && continue || { echo "please install $i for this script to work"; exit 1; }
done
dogrep() {
sudo grep -h "$today.*Ban " /var/log/fail2ban.log | tail -$number | awk -F[\ \:] '{print $19,$17}' | sort | uniq -c | awk '{cmd="curl -s https://ipvigilante.com/"$2"|jq -j \"[.data.city_name//empty, .data.country_name//empty] | @csv\"";cmd|getline country;close(cmd)}{cmd2="sudo grep "$2" /var/log/fail2ban.log | tail -1|cut -f1,2 -d \" \" --output-delimiter \"-\"";cmd2|getline timestamp;close(cmd2)}{print timestamp,$1,$2,$3,country}' | sort
}
printUsage() {
printf "Parameters:\n"
printf " -n[n] to list n ip addresses\n"
printf " -t to show output for only today\n"
}
while getopts ":n:t" optname
do
case "$optname" in
"n")
printf "Option $optname has value $OPTARG\n"
number=$OPTARG
;;
"t")
printf "Option $optname\n"
today=`date +%Y-%m-%d`
;;
"?")
printUsage
exit 0
;;
":")
printf "No argument value for option $OPTARG\n"
exit 1
;;
*)
# Should not occur
printf "Unknown error while processing options\n"
exit 1
;;
esac
done
if [ $OPTIND -eq 1 ]; then
printUsage
exit 0
else
dogrep
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment