スクリプト[冗長]で、システムにiptablesが設定されていることを確認してください。

スクリプト[冗長]で、システムにiptablesが設定されていることを確認してください。

Bashでスクリプトを作成していますが、iptablesが設定されていることを確認してください。以下があります。

if [ `iptables-save | grep '^\-' | wc -l` > 0 ]
then
    echo "Iptables already set, skipping..........!"
else
    Here the iptables get set

しかし、期待どおりに動作しません...

質問:

私はこれをし、iptables-saveこれを作りました:

$iptables-save
# Generated by iptables-save v1.6.0 on Tue Oct 16 02:48:41 2018
*filter
:INPUT ACCEPT [2:266]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2:116]
COMMIT
# Completed on Tue Oct 16 02:48:41 2018

したがって、テストを実行すると、次のように設定されていることがわかります。

(root@notemDEB78)-(03:12:20)-(/home/something78/Bash_Programming_2018)
$s.sh
Iptables already set....skipping!!!!!
(root@notemDEB78)-(03:12:25)-(/home/something78/Bash_Programming_2018)
$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 

(root@notemDEB78)-(03:16:41)-(/home/something78/Bash_Programming_2018)
$iptables-save | grep '^\-' | wc -l
0

質問:

  • iptablesが設定されているのに明らかに設定されていないのはなぜですか?
  • iptables設定されていることを確認するより良い効率的な方法はありますか?私のシステムは次のとおりです。Debian 9

    bash -version GNU bash, バージョン 4.4.12(1)-リリース(x86_64-pc-linux-gnu) 著作権 (C) 2016 Free Software Foundation, Inc.ライセンスGPLv3+:GNU GPLバージョン3以降http://gnu.org/licenses/gpl.html

可能な冗長性については次のとおりです。

この質問はまた、bashのスクリプトにiptablesが設定されているかどうかを確認するより良い方法があるかどうかを尋ねます。

編集する:

私が言う設定は、次のようにiptablesの設定を参照します。

if [[ `iptables-save | grep '^\-' | wc -l` > 0 ]]
    then
        echo "Iptables already set....skipping!!!!!"
    else
        if [ "$PORT" = "" ]
        then
            echo "Port not set for iptables exiting"
            echo -n "Setting port now, insert portnumber: "
            read port
            PORT=$port
        fi
        if [ ! -f /etc/iptables.test.rules ]
        then
            touch /etc/iptables.test.rules
        else
            cat /dev/null > /etc/iptables.test.rules
        fi

        cat << EOT >> /etc/iptables.test.rules
        *filter

        # Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
        -A INPUT -i lo -j ACCEPT
        -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

        # Accepts all established inOAUTH_TOKEN=d6637f7ccf109a0171a2f55d21b6ca43ff053616bound connections
        -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

        # Allows all outbound traffic
        # You could modify this to only allow certain traffic
        -A OUTPUT -j ACCEPT

        # Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
        -A INPUT -p tcp --dport 80 -j ACCEPT
        -A INPUT -p tcp --dport 443 -j ACCEPT

        # Allows SSH connections
        # The --dport number is the same as in /etc/ssh/sshd_config
        -A INPUT -p tcp -m state --state NEW --dport $PORT -j ACCEPT

        # Now you should read up on iptables rules and consider whether ssh access
        # for everyone is really desired. Most likely you will only allow access from certain IPs.

        # Allow ping
        #  note that blocking other types of icmp packets is considered a bad idea by some
        #  remove -m icmp --icmp-type 8 from this line to allow all kinds of icmp:
        #  https://security.stackexchange.com/questions/22711
        -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

        # log iptables denied calls (access via dmesg command)
        -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

        # Reject all other inbound - default deny unless explicitly allowed policy:
        -A INPUT -j REJECT
        -A FORWARD -j REJECT

        COMMIT
EOT
        sed "s/^[ \t]*//" -i /etc/iptables.test.rules ## remove tabs and spaces
        /sbin/iptables-restore < /etc/iptables.test.rules || echo "iptables-restore failed"; exit 127
        /sbin/iptables-save > /etc/iptables.up.rules || echo "iptables-save failed"; exit 127
        printf "#!/bin/bash\n/sbin/iptables-restore < /etc/iptables.up.rules" > /etc/network/if-pre-up.d/iptables ## create a script to run iptables on startup
        chmod +x /etc/network/if-pre-up.d/iptables || echo "cmod +x failed"; exit 127
    fi

ベストアンサー1

1つの可能な単純化は、独自の戻り値をgrep条件として使用してgrep一致する場合にのみ、コード0(「成功」を示す)で終了することです。探しているので、行数を数える必要はありません。どのマッチ。

-また、エスケープ\はgrep正規表現のメタ文字ではないため、使用する必要はありません。

一致する行を印刷しないようにパラメータを渡すことができます。 (一致する行があるかどうかによって、適切な終了コードで終了しますgrep。)-q

if /sbin/iptables-save | grep -q '^-'; then
    echo "Iptables already set....skipping!!!!!"
else
    # set up iptables here
fi

ルールが設定されていることを確認するために出力を調べるのは大丈夫だと思いますiptables-save。より安定しているか簡単な他の方法は考えられません。

おすすめ記事