Bash
檢查是否在腳本中的系統上設置了 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)-release (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
一種可能的簡化是使用
grep
自身的返回作為條件,grep
僅當存在匹配時才會以程式碼 0(表示“成功”)退出。由於您正在尋找任何匹配項,因此無需計算行數。此外,您不需要
-
用 a轉義\
,因為它不是 grep 正則表達式的元字元。您可以傳遞
grep
一個-q
參數,因此它不會列印匹配的行(僅使用適當的退出程式碼退出,具體取決於是否有任何行匹配。)if /sbin/iptables-save | grep -q '^-'; then echo "Iptables already set....skipping!!!!!" else # set up iptables here fi
我認為檢查輸出
iptables-save
以檢查是否設置了任何規則是可以的,我想不出另一種可靠或更簡單的方法。