block_ip.sh and Debian

My sample file I'm using

Code:
#!/bin/sh

BF=/root/blocked_ips.txt
EF=/root/exempt_ips.txt

curriptables()
{
        echo "<br><br><textarea cols=160 rows=60>";
        /sbin/iptables -nL
        echo "</textarea>";
}

COUNT=`grep -c "^${ip}\$" $EF`;
if [ "$COUNT" -ne 0 ]; then
        echo "$ip in the exempt list ($EF). Not blocking.";
        curriptables
        exit 2;
fi

COUNT=`grep -c $ip /etc/network/iptables.save`;
if [ "$COUNT" -ne 0 ]; then
        echo "$ip already exists in iptables ($COUNT). Not blocking.";
        curriptables
        exit 2;
fi

echo "Adding $ip to iptables...<br>";
/sbin/iptables -I INPUT -s $ip -j DROP
/sbin/iptables-save > /etc/network/iptables.save
echo "$ip=dateblocked=`date +%s`" >> $BF;

echo "<br><br>Result:";
curriptables
exit 0;
 
Back
Top