#!/bin/bash

echo ” “;
echo “USAGE :  /scripts/block_ipaddress ip1 ip2 ….”
echo ” “;

for i in $*
do

echo $i;

CHECK_IF_LOCALIP=0;
/sbin/ifconfig | grep $i > /dev/null;
if [ $? -ne $CHECK_IF_LOCALIP ]
then
{
FLAG=0;
grep $i /etc/sysconfig/iptables | grep DROP  > /dev/null;
if [ $? -ne $FLAG ]
then
printf ” \nBLOCKING ALL CONNECTIONS FROM  $i \t\n”;
iptables -I INPUT -p tcp -s $i -j DROP;
/etc/rc.d/init.d/iptables save;
printf “All connections from  $i is now blocked \t\n”;
else
printf ” \t\tIpaddress $i is already blocked \n”;
fi
}
else
echo ” Sorry, the ip $i cannot be blocked since this is a local ip of the server “;
fi

echo ” “;

done