rendered paste body#!/bin/sh
set -x
MAILTO="f4fahmed@gmail.com"
MAXLIMIT="0"
APF=`which apf`
NETSTAT=`which netstat`
IPTABLES=`which iptables`
NOW=`date +%s`
DATE=`date`
DIR="/root/block/ip"
ALLOW="/root/block/allow.txt"
if [[ ! -d "$DIR" ]] || [[ ! -f "$ALLOW" ]]
then
mkdir -p $DIR
touch $ALLOW
fi
#$NETSTAT -na | grep :80 | grep ESTA | awk ' { print $5 ; } ' | cut -d':' -f1 | sort | uniq -c | sort -nr -k1 | head -20 | while read LIMIT IP;
$NETSTAT -na | grep :80 | awk ' { print $5 ; } ' | cut -d':' -f1 | sort | uniq -c | sort -nr -k1 | head -20 | while read LIMIT IP;
do
while read IP1;
do
if [[ $IP1 = */* ]]
then
ALLIP=`cut -d. -f1-3 << $IP1`
else
ALLIP=$IP1
fi
done < $ALLOW
if [[ "$LIMIT" -gt "$MAXLIMIT" ]]
then
if [[ $IP != $ALLIP ]]
then
if [[ ! -f "$DIR/$IP" ]]
then
touch $DIR/$IP
apf -d $IP
echo "IP $IP was blocked" | mail -s "IP blocked : $IP" $MAILTO
fi
fi
fi
done
for test in `ls $DIR`
do
if [[ ! -z $(find $DIR -type f -amin +30) ]]
then
apf -u $test
rm -f $DIR/$test
echo "IP Address : $test was UnBlocked on Time : $DATE" | mail -s "IP unblocked : $test" $MAILTO
fi
done
set +x