Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  shell  >  正文 在shell中判断iptables是否开启的方法

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

发布时间:2014-11-26   编辑:www.jquerycn.cn
本文介绍下,通过shell脚本判断iptables是否开启的方法,有需要的朋友参考下。

如何用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
}

您可能感兴趣的文章:
在shell中判断iptables是否开启的方法
我的iptables配置脚本实例
管理iptables的shell脚本一例
CentOS下使用iptables开启端口
linux iptables 开启关闭端口的方法
linux iptables入门教程
二个iptables shell脚本(经典收藏)
shell脚本实现iptables防端口扫描
shell根据pid判断进程是否存在
shell条件测试语句实例-测试apache是否开启

[关闭]