Create /root/iptables/blocked.ips file as follows with list of ips and subnets to block entering your dedicated server:
192.168.1.0/24 202.54.1.2 # spam 202.5.1.2
Call following script from your existing shell script:
#!/bin/bash # Simple iptables IP/subnet block script # ------------------------------------------------------------------------- # Copyright (c) 2004 nixCraft project <http://www.cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ---------------------------------------------------------------------- IPT=/sbin/iptables SPAMLIST="spamlist" SPAMDROPMSG="SPAM LIST DROP" BADIPS=$(egrep -v -E "^#|^$" /root/iptables/blocked.ips) # create a new iptables list $IPT -N $SPAMLIST for ipblock in $BADIPS do $IPT -A $SPAMLIST -s $ipblock -j LOG --log-prefix "$SPAMDROPMSG" $IPT -A $SPAMLIST -s $ipblock -j DROP done $IPT -I INPUT -j $SPAMLIST $IPT -I OUTPUT -j $SPAMLIST $IPT -I FORWARD -j $SPAMLIST
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- 10 Greatest Open Source Software Of 2009
- My 10 UNIX Command Line Mistakes
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.
- Download Script
- Email this to a friend
- Rss Feed
- Last Updated: 12/20/08
{ 5 comments… read them below or add one }
How can I remove ip’s from blocked
I think rule
$IPT -I OUTPUT -j $SPAMLIST
is incorrect. Because in this case $ipblock is destination (-d), not source (-s)
hi
everybody now will come big platform
i wanna allow just this ip [10.0.0.100 - 10.0.0.112, 10.0.0.2, 10.0.0.3] and block other ip how is the configuration.
please send to my email : nseshop@gmail.com
thanks very much
hi i new to iptables where exactly should i write this script any help
please
thanks for any help appreciated.