I am writing a script that would output the info URL, Days Left for the SSL Cert and How many times the check failed. When check failed the 1st time, and you run it a 2nd time, it should increment the check fail count and update the count. I am using sed -i -e to change multiple values, separate by commas but for some reason, the whole like is not updated when the last -e value is present.
#!/usr/bin/env bash
# Script to check expiration date for SSL certificates.
usage () {
cat <<EOF
Check SSL Certificate Validity
Options:
-f File containing list of URLs to be checked.
-h Show this help info.
Sample Usage: $0 -f URL_List.txt
EOF
}
ReportFile="SSL_Validity_Report.rpt" # Sets Report File name.
while getopts ":f:h:" opt;
do
case $opt in
f) # Specify URL list file name in absolute path.
urlList=$OPTARG
;;
h) # Show help messages
Usage
exit
;;
*) # Alert wrong option and show help message.
echo -e "Error : Invalid Option Selected. n"
Usage
exit
;;
esac
done
ReportFileChecker () {
if [[ -f $ReportFile ]]; then # Checks if Report File exists.
if [[ -n $ReportFile ]]; then # Check if Report File is empty.
cp $ReportFile{,.prev} # Backup copy of previous day result.
FileExists="Y"
fi
else
touch $ReportFile # If Report File doesn't exist, create a blank file.
FileExists="N"
fi
}
ExpireChecker_N () { # Report file doesn't exist.
while read -r urlInfo ;
do
urlPath=$(echo $urlInfo)
OutputRemote=$(curl --insecure -vI $urlPath 2>&1
| awk 'BEGIN { cert=0 } /^* SSL connection/ { cert=1 } /^*/ { if (cert) print }'
| grep expire | cut -d ' ' -f4-8)
if [[ -n $OutputRemote ]]; then
DateExpire=$(date "+%d %b %Y %H:%M:%S %Z" -d "$OutputRemote")
DateExpireEpoch=$(date "+%s" -d "$DateExpire")
DateNowEpoch=$(date "+%s")
EpochTimeDiff=$(echo ""$DateExpireEpoch" - "$DateNowEpoch"" | bc)
DaysLeft=$(($EpochTimeDiff/86400))
CheckFailCount='0'
else
DateExpire='13 Dec 1989 05:17:00 EST'
DaysLeft="40000"
CheckFailCount='1'
fi
echo "$urlPath,$DateExpire,$DaysLeft,$CheckFailCount" >> "SSL_Validity_Report.rpt"
done < $urlList
}
ExpireChecker_Y () { # Report file exists.
while read -r urlInfo ;
do
urlPath=$(echo $urlInfo)
# Reading from report file
OutputFile=$(cat $ReportFile | grep "$urlPath")
urlPathFile=$(echo $OutputFile | awk -F ',' '{ print $1 }')
DateExpireFile=$(echo $OutputFile | awk -F ',' '{ print $2 }')
DateExpireFileEpoch=$(date "+%s" -d "$DateExpireFile")
DaysLeftFile=$(echo $OutputFile | awk -F ',' '{ print $3 }')
CheckFailCountFile=$(echo $OutputFile | awk -F ',' '{ print $4 }')
LineNo=$(cat -n $ReportFile | grep "$urlPath" | awk '{ print $1 }' )
# Reading from Live URL
OutputRemote=$(curl --insecure -vI $urlPath 2>&1
| awk 'BEGIN { cert=0 } /^* SSL connection/ { cert=1 } /^*/ { if (cert) print }'
| grep expire | cut -d ' ' -f4-8)
if [[ -n $OutputRemote ]]; then
DateExpire=$(date "+%d %b %Y %H:%M:%S %Z" -d "$OutputRemote")
DateExpireEpoch=$(date "+%s" -d "$DateExpire")
DateNowEpoch=$(date "+%s")
EpochTimeDiff=$(echo ""$DateExpireEpoch" - "$DateNowEpoch"" | bc)
DaysLeft=$(($EpochTimeDiff/86400))
CheckFailCountLive='0'
else
DateExpire='13 Dec 1989 05:17:00 EST'
DaysLeft="40000"
CheckFailCountOut=$(($CheckFailCountFile+1))
if [[ "$CheckFailCountOut" -ge '3' ]]; then
Alert_Monitoring="Y"
else
Alert_Monitoring='N'
fi
fi
# Reading from report file
# Ensure we are comparing for the same URL
if [[ "$urlPath" == "$urlPathFile" ]]; then
# Compares Days Left value for Report File & Current Days.
# If values are different, replace line with updated values
# else remain the same.
if [[ "$DaysLeft" -ne "$DaysLeftFile" ]]; then
sed -i -e "${LineNo}s/${DateExpireFile}/${DateExpire}/"
-e "${LineNo}s/${DaysLeftFile}/${DaysLeft}/"
-e "${LineNo}s/$CheckFailCountFile/$CheckFailCountLive/" $ReportFile
# Output
echo "OUTPUT: $DateExpire, $DaysLeft, $CheckFailLive"
elif [[ "$CheckFailCountOut" -ge 1 ]]; then
sed -i -e "${LineNo}s/${DateExpireFile}/${DateExpire}/"
-e "${LineNo}s/${DaysLeftFile}/${DaysLeft}/"
-e "${LineNo}s/$CheckFailCountFile/$CheckFailCountOut/" $ReportFile
# Output
echo "OUTPUT: $DateExpire, $DaysLeft, $CheckFailCountOut "
else
true
fi
urlPath=''
urlPathFile=''
CheckFailCountFile=''
CheckFailCountLive=''
CheckFailCountOut=''
else # What to do if URL doesn't match.
echo -e "$urlPath,${DateExpire},${DaysLeft},$CheckFailCount" >> $ReportFile
fi
done < $urlList
# Sorts Report File to ensure list is alphabetically ordered for next run.
sort -u -o $ReportFile{,}
}
# MAIN BODY
# Sort URL List to alphabetical order
sort -u -o $urlList{,}
# Run Report File checker function
ReportFileChecker
if [[ $FileExists = 'N' ]]; then
ExpireChecker_N
elif [[ $FileExists = 'Y' ]]; then
ExpireChecker_Y
else
echo "ERROR : Main Loop Break."
fi