首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IPTABLES,批量添加

IPTABLES,批量添加
EN

Ask Ubuntu用户
提问于 2021-08-12 10:25:25
回答 1查看 367关注 0票数 0

也许有人知道我如何用命令将140万ip添加到iptable:

iptables -I PREROUTING -t raw -d $ipban -j DROP

现在我使用:

代码语言:javascript
复制
while read ipban
do
iptables -I PREROUTING -t raw -d $ipban -j DROP
done < ips.txt

但它已经花了20多个小时才加进去。

我的vps非常小,像1gb的内存和1 1vcpu,所以它没有那么快。

我试着做iptables恢复,但我没有什么错误,所以我寻找最快的解决方案。

EN

回答 1

Ask Ubuntu用户

回答已采纳

发布于 2021-08-12 13:17:40

下面的脚本应该可以满足您的需要。注意,您将从其中读取is的文件的名称是脚本中的ips.txt。您可以用自己的文件名替换它。

代码语言:javascript
复制
#!/bin/bash

ip_addresses=$(cat ips.txt)

echo -n "" > iptables_configuration
echo "*raw" >> iptables_configuration
echo ":PREROUTING ACCEPT [0:0]" >> iptables_configuration
echo ":OUTPUT ACCEPT [0:0]" >> iptables_configuration

for i in $ip_addresses
do
echo -A PREROUTING -d $i/32 -j DROP >> iptables_configuration
done
echo "COMMIT" >> iptables_configuration

echo "*filter" >> iptables_configuration
echo ":INPUT ACCEPT [0:0]" >> iptables_configuration
echo ":FORWARD ACCEPT [0:0]" >> iptables_configuration
echo ":OUTPUT ACCEPT [0:0]" >> iptables_configuration
echo "-A INPUT -p tcp -m tcp --dport 25565 --tcp-option 8 --tcp-flags FIN,SYN,RST,ACK SYN -j REJECT --reject-with icmp-port-unreachable" >> iptables_configuration
echo "COMMIT" >> iptables_configuration

cat iptables_configuration | iptables-restore
rm iptables_configuration

iptables -t raw -A PREROUTING -p tcp --dport 25565 -j ACCEPT
iptables -t raw -A PREROUTING -p tcp --dport 25565 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t raw -A PREROUTING -p tcp --dport 25565 --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 150 --connlimit-mask 32 --connlimit-saddr -j DROP
iptables -t raw -A PREROUTING -p tcp --dport 25565 --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 10 --connlimit-mask 32 --connlimit-saddr -j DROP

您所要做的就是执行ips.txt文件所在的脚本。其余的将由脚本处理。

票数 0
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1357552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档