I've written a fairly basic script to try and stop brute force ssh
attacks that get up to thousands of user/password combinations. 
The script works fine for the most part, but requires refinement, that
isn't the point however.  The problem is that when the script is
run as a cron job, it completely ignores anything to do with iptables,
but the rest of the script does what it is supposed to do.  If I manually execute the script with sudo it works perfectly.<br>
<br>
#!/bin/bash<br>
#this script will read contents of auth.log and ban any ip which fails access<br>
# more than 10 times.<br>
#clear the current contents of iptables INPUT chain<br>
#iptables -F INPUT<br>
<br>
#get all the unique ip's and how many times they appear from auth.log<br>
cat /var/log/auth.log |<br>
grep Failed |<br>
grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' |<br>
uniq -d -c |<br>
while read count ip<br>
do<br>
  if [[ $count -gt 10 ]]<br>
    #add any IPs with more than 10 failed attempts iptables for dropping<br>
    then iptables -A INPUT -s $ip -j DROP<br>
  fi<br>
done<br>
<br>
echo "IP's have been blocked: $(date)" >> /home/simon/blockingscript.log<br>
<br>
exit 0<br>