iptables脚本生成工具
时间: 2023-11-11 15:07:46 浏览: 37
您可以使用iptables-save命令生成当前iptables规则的脚本。这个命令将当前的iptables规则以文本形式输出到终端上,您可以将其保存到一个文件中。如果您想要自动生成iptables脚本,可以使用iptables-save命令并将其输出重定向到一个文件,这样您就可以在以后使用这个文件来还原iptables规则了。例如,您可以使用以下命令将当前的iptables规则保存到一个名为iptables.rules的文件中:
```
iptables-save > iptables.rules
```
当您希望还原iptables规则时,可以使用以下命令将iptables规则从文件中加载:
```
iptables-restore < iptables.rules
```
这将从文件中读取iptables规则并将其还原到系统中。请注意,这将覆盖当前的iptables规则,因此请谨慎操作。
相关问题
linux iptables脚本
下面是一个简单的iptables脚本,可以阻止所有入站流量,允许所有出站流量:
```
#!/bin/bash
# 清空所有规则
iptables -F
# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
# 允许回环接口(localhost)
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# 允许已经建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 允许ssh连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许ping
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 允许HTTP和HTTPS连接
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 防止DOS攻击
iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT
# 允许DNS查询
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT
# 允许NTP时间同步
iptables -A INPUT -p udp --dport 123 -j ACCEPT
# 允许SMTP邮件
iptables -A INPUT -p tcp --dport 25 -j ACCEPT
# 允许POP3邮件
iptables -A INPUT -p tcp --dport 110 -j ACCEPT
# 允许IMAP邮件
iptables -A INPUT -p tcp --dport 143 -j ACCEPT
# 允许FTP连接
iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT
# 允许MySQL连接
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
# 允许SSH连接
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许Git连接
iptables -A INPUT -p tcp --dport 9418 -j ACCEPT
# 允许SVN连接
iptables -A INPUT -p tcp --dport 3690 -j ACCEPT
# 允许Redis连接
iptables -A INPUT -p tcp --dport 6379 -j ACCEPT
# 允许Memcached连接
iptables -A INPUT -p tcp --dport 11211 -j ACCEPT
# 允许MongoDB连接
iptables -A INPUT -p tcp --dport 27017 -j ACCEPT
# 允许Elasticsearch连接
iptables -A INPUT -p tcp --dport 9200 -j ACCEPT
iptables -A INPUT -p tcp --dport 9300 -j ACCEPT
# 允许Zookeeper连接
iptables -A INPUT -p tcp --dport 2181 -j ACCEPT
# 允许Kafka连接
iptables -A INPUT -p tcp --dport 9092 -j ACCEPT
# 允许Kibana连接
iptables -A INPUT -p tcp --dport 5601 -j ACCEPT
# 允许RabbitMQ连接
iptables -A INPUT -p tcp --dport 5672 -j ACCEPT
# 允许Zabbix连接
iptables -A INPUT -p tcp --dport 10050 -j ACCEPT
# 允许Prometheus连接
iptables -A INPUT -p tcp --dport 9090 -j ACCEPT
# 允许Grafana连接
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
# 允许Jenkins连接
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
# 允许Docker连接
iptables -A INPUT -p tcp --dport 2375 -j ACCEPT
iptables -A INPUT -p tcp --dport 2376 -j ACCEPT
# 保存规则
iptables-save > /etc/sysconfig/iptables
```
这个脚本假定你的系统默认是没有任何防火墙规则的。如果你已经有一些防火墙规则,那么需要根据实际情况进行修改。
iptables开放端口的shell交互脚本
以下是一个简单的 shell 脚本,用于使用 iptables 开放端口:
```shell
#!/bin/bash
# 定义变量
PORT=8080
PROTOCOL=tcp
# 检查是否已经开放端口
if [[ $(iptables -L -n | grep $PORT | grep $PROTOCOL) ]]; then
echo "端口 $PORT 已经被开放!"
exit 0
fi
# 开放端口
iptables -I INPUT -p $PROTOCOL --dport $PORT -j ACCEPT
iptables-save > /etc/sysconfig/iptables
# 检查端口是否成功开放
if [[ $(iptables -L -n | grep $PORT | grep $PROTOCOL) ]]; then
echo "端口 $PORT 已经成功开放!"
else
echo "端口 $PORT 开放失败!"
fi
```
可以将上述脚本保存为 `open_port.sh` 文件,并使用以下命令在终端中执行:
```shell
$ chmod +x open_port.sh
$ sudo ./open_port.sh
```
其中 `sudo` 是必需的,因为需要以管理员权限执行 iptables 命令。另外,请根据实际需求修改 `PORT` 和 `PROTOCOL` 变量的值。