如何从脚本中检查 iptables 是否为空
How to check if iptables is empty from a script
我正在编写一个脚本,需要检查 iptables 是否为空。
我唯一的想法是 "iptables-save" 并将其与空 iptables 的 "iptables-save" 进行比较。
但是,我不确定我是否可以指望 "iptables-save" 在每个主机上为空 iptables 产生相同的结果。
有什么想法吗?
您是否尝试过 --list
(-L) 参数?
$ sudo 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
该输出显示没有防火墙条目。
如果它输出了你会看到这样的东西...
$ sudo iptables -n -L -v --line-numbers
Chain INPUT (policy DROP)
num target prot opt source destination
1 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID
2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy DROP)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
2 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID
3 TCPMSS tcp -- 0.0.0.0/0 0.0.0.0/0 tcp flags:0x06/0x02 TCPMSS clamp to PMTU
4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
5 wanin all -- 0.0.0.0/0 0.0.0.0/0
6 wanout all -- 0.0.0.0/0 0.0.0.0/0
7 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
Chain wanin (1 references)
num target prot opt source destination
Chain wanout (1 references)
num target prot opt source destination
编辑:这是您可以使用的单行线:echo "$(sudo iptables -n -L -v --line-numbers)" | egrep "^[0-9]"
。然后测试 $?为 1 或 0。如果为 0,则有活动的防火墙规则,如果为 1,则没有活动的规则。
$ echo "$(sudo iptables -n -L -v --line-numbers)" | egrep "^[0-9]"
1 0 0 DROP all -- * * 202.54.1.2 0.0.0.0/0
$
$ echo $?
0
$
$ sudo iptables -D INPUT 1
$
$ echo "$(sudo iptables -n -L -v --line-numbers)" | egrep "^[0-9]"
$ echo $?
1
编辑:或者使用 wc -l 可能更好....
echo "$(sudo iptables -n -L -v --line-numbers)" | egrep "^[0-9]" | wc -l
对于以 -
开头的行,您可以使用 iptables-save
和 grep:
清空 iptables:
sudo iptables-save | grep '^\-' | wc -l
0
非空:
sudo iptables-save | grep '^\-' | wc -l
13
我正在编写一个脚本,需要检查 iptables 是否为空。
我唯一的想法是 "iptables-save" 并将其与空 iptables 的 "iptables-save" 进行比较。
但是,我不确定我是否可以指望 "iptables-save" 在每个主机上为空 iptables 产生相同的结果。
有什么想法吗?
您是否尝试过 --list
(-L) 参数?
$ sudo 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
该输出显示没有防火墙条目。
如果它输出了你会看到这样的东西...
$ sudo iptables -n -L -v --line-numbers
Chain INPUT (policy DROP)
num target prot opt source destination
1 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID
2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy DROP)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
2 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID
3 TCPMSS tcp -- 0.0.0.0/0 0.0.0.0/0 tcp flags:0x06/0x02 TCPMSS clamp to PMTU
4 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
5 wanin all -- 0.0.0.0/0 0.0.0.0/0
6 wanout all -- 0.0.0.0/0 0.0.0.0/0
7 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
Chain wanin (1 references)
num target prot opt source destination
Chain wanout (1 references)
num target prot opt source destination
编辑:这是您可以使用的单行线:echo "$(sudo iptables -n -L -v --line-numbers)" | egrep "^[0-9]"
。然后测试 $?为 1 或 0。如果为 0,则有活动的防火墙规则,如果为 1,则没有活动的规则。
$ echo "$(sudo iptables -n -L -v --line-numbers)" | egrep "^[0-9]"
1 0 0 DROP all -- * * 202.54.1.2 0.0.0.0/0
$
$ echo $?
0
$
$ sudo iptables -D INPUT 1
$
$ echo "$(sudo iptables -n -L -v --line-numbers)" | egrep "^[0-9]"
$ echo $?
1
编辑:或者使用 wc -l 可能更好....
echo "$(sudo iptables -n -L -v --line-numbers)" | egrep "^[0-9]" | wc -l
对于以 -
开头的行,您可以使用 iptables-save
和 grep:
清空 iptables:
sudo iptables-save | grep '^\-' | wc -l
0
非空:
sudo iptables-save | grep '^\-' | wc -l
13