Bash 检查iptables用户链是否存在的最佳方法。
我试图以编程方式创建用户链并在iptables中删除它们。我想知道检查用户链是否存在以及是否没有创建它的最佳方法是什么 使用Bash 检查iptables用户链是否存在的最佳方法。,bash,shell,iptables,Bash,Shell,Iptables,我试图以编程方式创建用户链并在iptables中删除它们。我想知道检查用户链是否存在以及是否没有创建它的最佳方法是什么 使用iptables(8)列出链,将stdout/stderr重定向到/dev/null,并检查退出代码。如果链存在,iptables将退出true 此shell函数来自我的iptables前端脚本: chain_exists() { [ $# -lt 1 -o $# -gt 2 ] && { echo "Usage: chain_ex
iptables(8)
列出链,将stdout/stderr重定向到/dev/null
,并检查退出代码。如果链存在,iptables
将退出true
此shell函数来自我的iptables前端脚本:
chain_exists()
{
[ $# -lt 1 -o $# -gt 2 ] && {
echo "Usage: chain_exists <chain_name> [table]" >&2
return 1
}
local chain_name="$1" ; shift
[ $# -eq 1 ] && local table="--table $1"
iptables $table -n --list "$chain_name" >/dev/null 2>&1
}
其中create\u chain
是创建链的另一个函数。您可以直接调用iptables
,但是上面的命名使事情变得非常明显。应该是iptables(8)
和iptables-t$table…
?@mkj:iptables(8)-我已经解决了这个问题,谢谢-t$table
-查看上面的行,您将看到$table
已经包含--table
。我这样做是为了$table
要么为空,要么为“-table”,因为该表是可选的,如果没有表参数,您就不需要-t/--table。
chain_exists foo || create_chain foo ...