判断iptables是否运行的一些探索

现在有这么一个需求,要判断iptables是否开启。咋看比较难以入手,那么可以想,怎么启动iptables,redhat下很容易联想到/etc/init.d/iptabes start 。

我们可以来看一下启动脚本:

start() {
    # Do not start if there is no config file.
    [ ! -f "$IPTABLES_DATA" ] && return 6

    # check if ipv6 module load is deactivated
    if [ "${_IPV}" = "ipv6" ] \
    && grep -qIsE "^install[[:space:]]+${_IPV}[[:space:]]+/bin/(true|false)" /etc/modprobe.conf /etc/modprobe.d/* ; then
    echo $"${IPTABLES}: ${_IPV} is disabled."
    return 150
    fi

    echo -n $"${IPTABLES}: Applying firewall rules: "

    OPT=
    [ "x$IPTABLES_SAVE_COUNTER" = "xyes" ] && OPT="-c"

    $IPTABLES-restore $OPT $IPTABLES_DATA
    if [ $? -eq 0 ]; then
    success; echo
    else
    failure; echo;
    if [ -f "$IPTABLES_FALLBACK_DATA" ]; then
        echo -n $"${IPTABLES}: Applying firewall fallback rules: "
        $IPTABLES-restore $OPT $IPTABLES_FALLBACK_DATA
        if [ $? -eq 0 ]; then
        success; echo
        else
        failure; echo; return 1
        fi
    else
        return 1
    fi
    fi
   
    # Load additional modules (helpers)
    if [ -n "$IPTABLES_MODULES" ]; then
    echo -n $"${IPTABLES}: Loading additional modules: "
    ret=0
    for mod in $IPTABLES_MODULES; do
        echo -n "$mod "
        modprobe $mod > /dev/null 2>&1
        let ret+=$?;
    done
    [ $ret -eq 0 ] && success || failure
    echo
    fi
   
    # Load sysctl settings
    load_sysctl

    touch $VAR_SUBSYS_IPTABLES
    return $ret
}

这个函数其实核心点就是iptables-restore  ,没有后台启动一个daemon程序那种, 因为iptables 是linux 的一个内核模块。这样就比较难判断了,因为不能用所谓的ps查进程的方式,当然末尾有个lock文件可以判断,但如果不是用启动脚本启动的,直接iptables-restore,则不会产生这个lock文件。

至此,陷入一个难题中。

posted @ 2014-11-01 12:27  过去的我  阅读(743)  评论(0编辑  收藏  举报