2017-08-08 78 views
1

我目前正在做一個簡單的安全審計腳本,它將打印每個配置不匹配的OK /錯誤。 情景是未來 - 例如,對於sshd_config中我已經把未來如果情況:SSHD配置檢查bash腳本

if [ "`grep -E ^Protocol /etc/ssh/sshd_config`" == "Protocol 1" ]; then 
    echo "Protocol should be set to 2"; 
fi 

的問題是 - 如果什麼有一些變量及其值之間的一個以上的空間? (協議2或PermitRootLogin否,例如); /*s/s和類似的技巧沒有幫助。

有沒有人有一個樣本來檢查bash腳本中的SSHD配置以在這裏發佈案例? 在此先感謝!

回答

3

grep的-E選項將其放入擴展的正則表達式模式中。所以,你可以使用擴展正則表達式(^Protocol +1意味着開始Protocol線,隨後1個或多個空格,然後性格1):

if grep -Eq '^Protocol +1' /etc/ssh/sshd_config; then 
    echo "Protocol should be set to 2"; 
fi 

[yY]意味着字符yY

if grep -Eq '^PermitEmptyPasswords +[yY][eE][sS]' /etc/ssh/sshd_config; then 
    echo "PermitEmptyPasswords should be set to no"; 
elif grep -Eq '^PermitEmptyPasswords +[nN][oO]' /etc/ssh/sshd_config; then 
    echo "PermitEmptyPasswords meets requirements"; 
fi 

和許多其他有趣的功能。備註:

  • 您應該考慮在sshd_config文件中有多個匹配行的情況。
  • -q grep選項禁止打印匹配的行。如果找到了匹配的行,grep只會以狀態0退出,否則狀態爲1(未找到)或2(錯誤)。
  • if語句後面可以緊跟一個要執行的命令列表。如果列表的退出狀態爲0,則採取then分支。在我們的案例中,列表只是grep
+0

如何把案例Yes和No(例如PermitEmptyPasswords)呢? 例如: if grep -Eq'^ PermitEmptyPasswords + yes'/ etc/ssh/sshd_config;那麼 回聲「價值不符合要求」; 其他回聲「價值確實符合要求」<<< - 在這個地方輸入什麼,一個案例還是一個案例,如果是「否」值? –

+0

@Kristian將此添加到我的答案。試試也許找到擴展正則表達式的文檔,這是一個非常有用的知識。 –

+0

感謝您的提示,我是腳本編寫新手,所以有些程序現在還不清楚1/1。 –

0


試試這個:

if [[ $(cat /etc/ssh/sshd_config | awk '/^Protocol/{print $2}') != 2 ]]; then 
    echo "Protocol should be set to 2" 
fi 
0

使用grep得到協議2行。

然後取出協議1線,其可包括2作爲協議2,1

這樣最後的散列應該排除。最後回顯需要的字符串。

在grep的加入-i在grep命令不區分大小寫

grep -i "Protocol 2" /etc/ssh/sshd_config | grep -v "#"|grep -v "Protocol 1"|| echo "Protocol 2 is needed"