我正在 bash 中编写一个脚本,我需要检查 iptables 是否已设置...我有这个:
if [ `iptables-save | grep '^\-' | wc -l` > 0 ]
then
echo "Iptables already set, skipping..........!"
else
Here the iptables get set
但它没有按预期工作......
问题:
我这样做了iptables-save
,它创造了这个:
$iptables-save
# Generated by iptables-save v1.6.0 on Tue Oct 16 02:48:41 2018
*filter
:INPUT ACCEPT [2:266]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2:116]
COMMIT
# Completed on Tue Oct 16 02:48:41 2018
因此,如果我运行测试,它会发现它们已经设置......如下所示:
(root@notemDEB78)-(03:12:20)-(/home/something78/Bash_Programming_2018)
$s.sh
Iptables already set....skipping!!!!!
(root@notemDEB78)-(03:12:25)-(/home/something78/Bash_Programming_2018)
$iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
(root@notemDEB78)-(03:16:41)-(/home/something78/Bash_Programming_2018)
$iptables-save | grep '^\-' | wc -l
0
问题:
- 为什么总是发现 iptables 已设置,而明明没有设置
你有更好/有效的方法来检查是否
iptables
已设置......我的系统是Debian 9
bash -version GNU bash,版本 4.4.12(1)-release (x86_64-pc-linux-gnu) 版权所有 (C) 2016 Free Software Foundation, Inc. 许可证 GPLv3+:GNU GPL 版本 3 或更高版本http://gnu.org/licenses/gpl.html
至于可能的重复:
这个问题还询问是否有人有更好的方法来检查 iptables 是否为 bash 中的脚本设置。
编辑:
我所说的设置是指 iptables 的设置如下:
if [[ `iptables-save | grep '^\-' | wc -l` > 0 ]]
then
echo "Iptables already set....skipping!!!!!"
else
if [ "$PORT" = "" ]
then
echo "Port not set for iptables exiting"
echo -n "Setting port now, insert portnumber: "
read port
PORT=$port
fi
if [ ! -f /etc/iptables.test.rules ]
then
touch /etc/iptables.test.rules
else
cat /dev/null > /etc/iptables.test.rules
fi
cat << EOT >> /etc/iptables.test.rules
*filter
# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
# Accepts all established inOAUTH_TOKEN=d6637f7ccf109a0171a2f55d21b6ca43ff053616bound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allows all outbound traffic
# You could modify this to only allow certain traffic
-A OUTPUT -j ACCEPT
# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
# Allows SSH connections
# The --dport number is the same as in /etc/ssh/sshd_config
-A INPUT -p tcp -m state --state NEW --dport $PORT -j ACCEPT
# Now you should read up on iptables rules and consider whether ssh access
# for everyone is really desired. Most likely you will only allow access from certain IPs.
# Allow ping
# note that blocking other types of icmp packets is considered a bad idea by some
# remove -m icmp --icmp-type 8 from this line to allow all kinds of icmp:
# https://security.stackexchange.com/questions/22711
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# log iptables denied calls (access via dmesg command)
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound - default deny unless explicitly allowed policy:
-A INPUT -j REJECT
-A FORWARD -j REJECT
COMMIT
EOT
sed "s/^[ \t]*//" -i /etc/iptables.test.rules ## remove tabs and spaces
/sbin/iptables-restore < /etc/iptables.test.rules || echo "iptables-restore failed"; exit 127
/sbin/iptables-save > /etc/iptables.up.rules || echo "iptables-save failed"; exit 127
printf "#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules" > /etc/network/if-pre-up.d/iptables ## create a script to run iptables on startup
chmod +x /etc/network/if-pre-up.d/iptables || echo "cmod +x failed"; exit 127
fi
答案1
一种可能的简化是使用自身的返回grep
作为条件,grep
仅当存在匹配时才会以代码 0(表示“成功”)退出。由于您正在寻找,因此无需计算行数任何匹配。
另外,您不需要使用-
a 转义\
,因为它不是 grep 正则表达式的元字符。
您可以传递grep
一个-q
参数,因此它不会打印匹配的行(仅根据是否有任何行匹配而以适当的退出代码退出。)
if /sbin/iptables-save | grep -q '^-'; then
echo "Iptables already set....skipping!!!!!"
else
# set up iptables here
fi
我认为检查输出来iptables-save
检查是否设置了任何规则是可以的,我想不出另一种更可靠或更简单的方法。
答案2
该iptables
命令有自己的方法来检查特定规则是否存在,并-C
带有选项:
-C, --check chain rule-specification
检查所选链中是否存在符合规范的规则。此命令使用与 -D 相同的逻辑来查找匹配条目,但不会更改现有的 iptables 配置并使用其退出代码来指示成功或失败。
例子:
if ! iptables -C INPUT ! -i lo -d 127.0.0.0/8 -j REJECT; then
iptables -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
fi
您当前的脚本检查表中是否有任何规则,但如果您想编写可靠的脚本,您可能希望/需要一一检查所有规则,因为其他程序iptables
也可以与之交互。