Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make shell script loop not stop on error

Tags:

scripting

I have a master shell script which called a child script for each iteration of a loop, like so:

#!/bin/bash

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

If any of those calls land in this case (see shell script below)

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

then the master script will stop and not call the next batch. How do I make it continue?

#!/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
like image 810
David Avatar asked Nov 14 '22 21:11

David


2 Answers

You can try this, the echo will always succeed if your other script fails.

if [[ $line != "" ]]
then
    ./sslv2_check.sh $line || echo "failed"
fi
like image 164
J.N. Avatar answered Dec 29 '22 00:12

J.N.


Once openssl connects, it waits for input before closing. I don't know why but this is causing the master batch script to abort. The solution is as follows:

replace

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

with

output=$(echo 'GET HTTP/1.0' | openssl s_client -connect $1:$port -ssl2 2>&1)
like image 25
David Avatar answered Dec 29 '22 00:12

David