#!/bin/bash
file="denyHosts.txt"
limit=3
if [ ! -f "$file" ]
then
touch "$file"
fi
cat /var/log/auth.log | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | sort -nr | awk '{print $1"-"$2}' > $file
for i in `cat $file`
do
num=`echo $i | awk -F- '{print int($1)}'`
ip=`echo $i | awk -F- '{print $2}'`
if [ "$num" -ge "$limit" ]
then
ipExist=`grep $ip /etc/hosts.deny -w`
if [ -z "$ipExist" ]
then
echo "sshd:$ip" >> /etc/hosts.deny
fi
fi
done
rm $file
sudo service sshd restart