Quick Script to Block an IP Address

From HowToGeek

Jump to: navigation, search

If you want to quickly block an IP address from connecting to your server using the iptables firewall, just use the following shell script.

Create a new script (as root) somewhere available in the path by using this command:

vi /usr/local/bin/blockip

Now paste in the following code:

#!/bin/sh

if [ "$1" != "" ]; then
        /sbin/iptables -I INPUT -s $1 -j DROP
else
        echo "Dude, like, Type an IP or something."
        echo "Usage:"
        echo "  blockip <ipaddress>"
        echo ""
fi

Once you are done, you need to make it executable by running the following command:

chmod u+x /usr/local/bin/blockip

Now you can use the script like this:

blockip 10.1.1.10

That's all there really is to it.