gpt4 book ai didi

scripting - 如何使 shell 脚本循环不会因错误而停止

转载 作者:行者123 更新时间:2023-12-05 01:30:09 25 4
gpt4 key购买 nike

我有一个主 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

最佳答案

一旦 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)

关于scripting - 如何使 shell 脚本循环不会因错误而停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6369090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com