2011-06-16 159 views
2

我有一個主shell腳本,稱爲子腳本,針對一循環的每次迭代,就像這樣:如何使shell腳本循環不停止錯誤

#!/bin/bash 

while read line 
do 
    if [[ $line != "" ]] 
    then 
     ./sslv2_check.sh $line 
    fi 
done < https-servers 

如果任何這些電話在這種情況下降落(請參閱下面的shell腳本)

message="FAIL! $1 supports SSLv2 on port $port" 

然後主腳本將停止並且不會調用下一批。我如何讓它繼續?

#!/bin/bash 

# Required Argument $1 = hostname 
# Optional Argument $1 = port number 
if [[ $1 == "" ]] 
then 
    echo Error: I expected a hostname to be passed as an argument but didn\'t find any 
    exit 1 
fi 

if [[ $2 == "" ]] 
then 
    port=443 
else 
    port=$2 
fi 

date=$(date +"%Y-%m-%d") 
datetime=$(date +"%Y-%m-%d-%H-%M") 
errorlogfile=logs/$date.error.log 
logfile=logs/$date.log 
# Testing for SSLv2 
output=$(openssl s_client -connect $1:$port -ssl2 2>&1) 
if [[ $output == *"handshake failure"* ]] 
then 
    message="PASS! SSLv2 not supported by $1 on port $port" 
elif [[ $output == *"104"* ]] 
then 
    message="PASS! SSLv2 is not supported by $1 on port $port" 
elif [[ $output == *"null ssl method passed"* ]] 
then 
    message="ERROR! SSLv2 is not enabled on your local machine" 
    # Log error 
    echo "$datetime -- $message" >> $errorlogfile 
    echo $output >> $errorlogfile 
elif [[ $output == *"110"* ]] 
then 
    message="ERROR! Failed to connect to $1. Make sure you type in the hostname correctly etc." 
    # Log error 
    echo "$datetime -- $message" >> $errorlogfile 
    echo $output >> $errorlogfile 
elif [[ $output == *"BEGIN CERTIFICATE"* ]] 
then 
    message="FAIL! $1 supports SSLv2 on port $port" 
    # Log error 
    echo "$datetime -- $message" >> $errorlogfile 
    echo $output >> $errorlogfile 
else 
    message="ERROR! An unknown error occurred. See $errorlogfile for details" 
    echo "$datetime -- $message" >> $errorlogfile 
    echo $output >> $errorlogfile 
fi 
#stdout the message 
echo $message 
#Log the message 
echo "$datetime -- $message" >> $logfile 

回答

0

一旦openssl連接,它會在關閉之前等待輸入。我不知道爲什麼,但是這導致主批處理腳本中止。該解決方案如下:

更換

output=$(openssl s_client -connect $1:$port -ssl2 2>&1) 

output=$(echo 'GET HTTP/1.0' | openssl s_client -connect $1:$port -ssl2 2>&1) 
0

您可以試試這個,如果您的其他腳本失敗,echo將始終成功。

if [[ $line != "" ]] 
then 
    ./sslv2_check.sh $line || echo "failed" 
fi 
+0

我只是有靈感的尷尬閃光:OpenSSL的等待交互式輸入,如果連接成功。所以這個修復它輸出= $(echo'GET HTTP/1.0'| openssl s_client -connect $ 1:$ port -ssl2 2>&1) – David 2011-06-16 08:55:35

+0

噢好吧,我沒有通過「停止」得到你的意思是「凍結」。 – 2011-06-16 23:28:38

+0

實際上並沒有凍結,它停了下來。就像實際返回到shell。 – David 2011-06-30 03:34:45