IPアドレスで構成されたファイルを読み込むためのシェルスクリプトを作成し、iptablesの助けを借りてブロックします。それはうまく動作しますが、スクリプトを2回実行すると、ルールが再度(重複して)書き込まれます。 IPがすでにブロックされているかどうかをチェックし、それ以外のブロックを無視するようにしたい。ここでのスクリプトは次のとおりです。初めてのスクリプトの実行後にファイルを読み取るためのシェルスクリプト
#!/bin/bash
ipadds="/home/asad/Downloads/blacklist"
dropit=$(grep -Ev "^#" $ipadds)
for i in $dropit; do
iptables -A INPUT -s $i -j DROP
iptables -A FORWARD -s $i -j DROP
done
出力:二度目のスクリプトの実行後に
[email protected]:/home/asad/Downloads# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.248.2 anywhere
DROP all -- 192.168.232.20 anywhere
DROP all -- 192.168.232.5 anywhere
DROP all -- 192.168.232.190 anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.248.2 anywhere
DROP all -- 192.168.232.20 anywhere
DROP all -- 192.168.232.5 anywhere
DROP all -- 192.168.232.190 anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
出力:この重複を避けるために、どのように
[email protected]:/home/asad/Downloads# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.248.2 anywhere
DROP all -- 192.168.232.20 anywhere
DROP all -- 192.168.232.5 anywhere
DROP all -- 192.168.232.190 anywhere
DROP all -- 192.168.248.2 anywhere
DROP all -- 192.168.232.20 anywhere
DROP all -- 192.168.232.5 anywhere
DROP all -- 192.168.232.190 anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.248.2 anywhere
DROP all -- 192.168.232.20 anywhere
DROP all -- 192.168.232.5 anywhere
DROP all -- 192.168.232.190 anywhere
DROP all -- 192.168.248.2 anywhere
DROP all -- 192.168.232.20 anywhere
DROP all -- 192.168.232.5 anywhere
DROP all -- 192.168.232.190 anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
?すべてのヘルプは
'while' – Inian
@gniourf_gniourfでコマンド出力、使用プロセス置換をループするために' for'を使用しないでください試みることができる:それはちょうどそれが問題を解決していません実現します。 – Inian
あなたの目的のコードはそのようには機能しません。コマンド出力を格納するための配列が必要でした。あなたの現在の構文では、シェルが単語の分割を行い、1つのスペースで区切られた行を複数のエントリに処理することができます – Inian