Iptables: script to block selected or specific ip addresses

Iptables: script to block selected or specific ip addresses

Iptables: script to block selected or specific ip addresses 150 150 Roderick Derks

Here I got this script for you that can help you to secure your server from attackers or bots.
Just add ip addresses you want to block to a file, on each line one, and run the script. Iptables will do the rest.

#!/bin/sh
#
# ip_block.sh
#
# block ip addresses from a blacklist
#
# www.r71.nl
# 20070215
#####################################

# file with on each line an ip address you want to block
BLOCKDB="/root/block_ip_list"

IPTABLES=/sbin/iptables

# show and remove duplicate ip addresses from the block_ip_list file
echo Removing duplicate ip addresses from block_ip_list:
sort $BLOCKDB | uniq -c  |grep -v " 1 "|grep -v ^$
sort $BLOCKDB | uniq  > $BLOCKDB

IPS=$(grep -Ev "^#" $BLOCKDB)
counter=0

for i in $IPS
do
  counter=$(($counter+1))
  $IPTABLES -A INPUT -s $i -j DROP
  $IPTABLES -A OUTPUT -d $i -j DROP
done

echo
echo
echo Number of blocked IP addresses: $counter
echo Have a secure day!

 

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Your Name (required)

    Your Email (required)

    Subject

    Your Message

      Your Name (required)

      Your Email (required)

      Subject

      Your Message