gpt4 book ai didi

linux - bash 脚本执行多个 iptables 链

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:29:38 26 4
gpt4 key购买 nike

我正在使用下面的脚本通过从 whitelist.txt 文件中过滤 IP 来应用 iptables

如果列表中有多个 IP,我的 iptables 会显示多个链:

#!/bin/bash

# allowed ip file location
WHITELIST=/usr/src/firewall/whitelist.txt
#
## Specify where IP Tables is located
#

IPTABLES=/sbin/iptables
IPTABLES_SAVE=/sbin/iptables-save

#
## Save current iptables running configuration in case we want to revert back
## To restore using our example we would run "/sbin/iptables-restore < /usr/src/iptables.last"
#
$IPTABLES_SAVE > /usr/src/iptables.last
#
## Clear current rules
#
##If current INPUT policy is set to DROP we will be locked out once we flush the rules
## so we must first ensure it is set to ACCEPT.
#
$IPTABLES -P INPUT ACCEPT
echo 'Setting default INPUT policy to ACCEPT'

$IPTABLES -F
echo 'Clearing Tables F'
$IPTABLES -X
echo 'Clearing Tables X'
$IPTABLES -Z
echo 'Clearing Tables Z'

#Always allow localhost.
echo 'Allowing Localhost'
$IPTABLES -A INPUT -s 127.0.0.1 -j ACCEPT

#
## Whitelist
#

for x in `grep -v ^# $WHITELIST | awk '{print $1}'`; do
echo "Permitting $x..."
# $IPTABLES -A INPUT -s $x -j ACCEPT
$IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT
$IPTABLES -A INPUT -p tcp -m tcp -s "$x" --dport 80 -j ACCEPT
$IPTABLES -A INPUT -p udp -m udp -s "$x" --dport 5060 -j ACCEPT
done

# block all other traffice

$IPTABLES -A INPUT -p all -j DROP
#
## Save the rules so they are persistent on reboot.
#
/etc/init.d/iptables save

我的 iptables -L -n 输出显示为

firewall]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 127.0.0.1 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 192.168.1.125 0.0.0.0/0 tcp dpt:80
ACCEPT udp -- 192.168.1.125 0.0.0.0/0 udp dpt:5060
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 192.168.1.1 0.0.0.0/0 tcp dpt:80
ACCEPT udp -- 192.168.1.1 0.0.0.0/0 udp dpt:5060
DROP all -- 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy DROP)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

如何避免重复,那个脚本有什么问题....

最佳答案

我猜你的 whitelist.txt 包含两个 IP:192.168.1.125 和 192.168.1.1?!

然后你给每个 IP 设置了三个规则,一个用于 SSH,一个用于 HTTP,一个用于 SIP,只是你没有为 SSH 指定 --source/-s ,因此对于白名单中的任何 IP,该规则自然与之前的规则相同。

TL;DR:将 -s "$x" 添加到 SSH 规则,您应该没问题。

额外提示:如果您想允许整个私有(private) C 类子网,您可以使用语法 -s 192.168.1.0/24 :-)

干杯,

关于linux - bash 脚本执行多个 iptables 链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29491002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com