I have a little script that I use to scan my log files for people trying to log in to ssh, once they get to a limit of attempts, I then use iptables to block them permantely.
Code:
#!/bin/sh
HOSTSDENY="/etc/hosts.deny"
BADCOUNT="1"
IPTABLES=`which iptables`
cat /var/log/messages | grep "user not found" | tr -c '.[:digit:]' '\n' | grep '^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$' | sort | uniq -c | sort -n | while read i
do
# read number of failed attempts
count=`echo $i | cut -d" " -f1`
# read ip address from failed attempt
ip=`echo $i | cut -d" " -f2`
#check hostdeny file to see if IP already exist
already=`grep $ip $HOSTSDENY`
#if IP does not exist add it to hostdeny file
if [ -z $already ]; then
if [ $count -ge $BADCOUNT ]; then
echo "$ip" >> $HOSTSDENY
# add a rule to iptables to drop
CHECK_IPTABLES=`$IPTABLES -L -n | grep "$ip"` # check first to see if a rule already exists
if [ -z $CHECK_IPTABLES ]; then
$IPTABLES -I INPUT -s $ip -j DROP
fi
fi
fi
done