利用iptable屏蔽特定网站(转载)

本文提供了一个bash脚本,用于从文本文件中读取IP地址,并使用iptables封禁这些IP地址,帮助管理员批量封禁特定网站。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//转载请出自出处:http://blog.youkuaiyun.com/david_luyang/archive/2011/04/10/6313448.aspx

 

#!/bin/sh
#Filename: block_ip.sh
# Purpose: blocks all IP address/network found in a text file
# The text file must have one IP address or network per line
#################################################################

# Change the following path/filename to match yours
IP_LIST_FILE=path/to/bad.ip.txt
#Follwing orders be used to maintain the file of above
sed -i '/Address/!d' $IP_LIST_FILE
sed -i '/#/d' $IP_LIST_FILE
sed -i 's/.........//' $IP_LIST_FILE

#################################################################
# Don't change anything below unless you are a smarty pant!
#################################################################
IPTABLES_BIN=/sbin/iptables

# Get the IP address/network from the file and ignore any line starting with # (comments)
BAD_IP_ADDR_LIST=$(grep -Ev "^#" $IP_LIST_FILE)

# Now loop through the IP address/network list and ban them using iptabels
for i in $BAD_IP_ADDR_LIST
do

echo -n "Blocking $i ...";
$IPTABLES_BIN -A INPUT -s $i -j DROP
$IPTABLES_BIN -A OUTPUT -d $i -j DROP

echo "DONE.";
done
#iptables -save >/etc/sysconfig/iptables
service iptables save
service iptables restart
echo "This is made by david_luyang.Thanks for using";
##################################################################
# END OF SCRIPT - NOTHING TO SEE HERE - THAT'S ALL FOLKS!
##################################################################

 

 

具体操作步骤如下:

1:#mkdir /path/to/file

2:#touch /path/to/file/bad.ip.txt

3:#nslookup www.***.com >> bad.ip.txt

4:重复第3步,直到你把所有想屏蔽的网站输入完毕

5:#sh /path/to/**.sh(你脚本的名字)

6:屏蔽工作完成

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值