You could also just have iptables do the tracking and heavy lifting for you. I've got these rules monitoring my ssh port:
Code:
iptables -N AUTOBAN
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j AUTOBAN
iptables -A AUTOBAN -m recent --set --name SSH
iptables -A AUTOBAN -m recent --update --seconds 120 --hitcount 4 --name SSH -j DROP
Basically it blocks any SSH connection attempts after 4 in the last 120 seconds. The downside is that it is kind of a broad-spectrum approach in that there isn't any sort of a whitelist and depending on what kind of traffic you have, you may wind up blocking legitimate traffic for a bit. However, for my personal use, I've found this easier to maintain than the script-based approaches.