Linux · 2010-03-04

批量封掉危险的IP网段

根据dshield.org的黑名单,可得到一堆危险的网段,中国的IP也占了一些,我看估计都是肉鸡.
防止这些危险的机器向我们发起攻击,决定把这些危险的IP网段封掉(debian测试通过).
当然,iptables也能做,但是在大流量攻击面前,无效路由显得更轻松,不会像iptables那样占用大量资源!

#!/bin/bash
# filename:  block_IPs.sh
# auth: baiqiuyi.com
curl "http://www.spamhaus.org/drop/drop.lasso"|awk 'NR>1{print $1}' >ip
if [ $? -eq 0 ];then
while read ip
   do
 /usr/bin/ipcalc $ip|sed -n '/Address/p;/Netmask/p'|sed -r 's/.*( ([0-9]{1,3}\.){3}[0-9]{1,3}).*/\1/'|sed 'N;s/\n/ netmask /g;s/^/route add -net/;s/$/ reject/'|bash
   done < ip
   rm -f ip
exit 0
 fi

附: ipcalc下载地址