当前位置: > shell编程 >

在shell中判断iptables是否开启的方法

时间:2014-12-05 02:32来源:linux.it.net.cn 作者:IT

如何用shell判断iptables服务是否开启呢?

问题:有如下的脚本,判断iptables是否开启,不知道是否正确?
代码:
 

/sbin/service iptables status 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
_firewall_status=”stopped”
fi

解答:
此脚本可以判断iptables服务是否开启。
首先$?在shell中表示上一个命令的返回值。往往0表示成功。非0表示失败。
其次,查看了下/etc/init.d/iptables脚本的内容(此查找路径仅保证在fedora中有效)。
有关status的代码,大概在275行。
在iptables关闭时下会返回3。成功则返回0。

附,iptables脚本中有关status函数的脚本内容:
 

status() {
if [ ! -f "$VAR_SUBSYS_IPTABLES" -a -z "$NF_TABLES" ]; then
echo $”${IPTABLES}: Firewall is not running.”
return 3
fi

# Do not print status if lockfile is missing and iptables modules are not
# loaded.
# Check if iptable modules are loaded
if [ ! -e "$PROC_IPTABLES_NAMES" ]; then
echo $”${IPTABLES}: Firewall modules are not loaded.”
return 3
fi

# Check if firewall is configured (has tables)
if [ -z "$NF_TABLES" ]; then
echo $”${IPTABLES}: Firewall is not configured. ”
return 3
fi

NUM=
[ "x$IPTABLES_STATUS_NUMERIC" = "xyes" ] && NUM=”-n”
VERBOSE=
[ "x$IPTABLES_STATUS_VERBOSE" = "xyes" ] && VERBOSE=”–verbose”
COUNT=
[ "x$IPTABLES_STATUS_LINENUMBERS" = "xyes" ] && COUNT=”–line-numbers”

for table in $NF_TABLES; do
echo $”Table: $table”
$IPTABLES -t $table –list $NUM $VERBOSE $COUNT && echo
done

return 0
}

 


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容