Friday, 7 August 2015

SSL Certificate Check and Email Using Bash Script

#!/bin/bash
TodayPlus30=`date +%d" "%b" "%Y  -d '+30 day'`
TodayPlus15=`date +%d" "%b" "%Y  -d '+15 day'`
TodayPlus7=`date +%d" "%b" "%Y  -d '+7 day'`
TodayPlus1=`date +%d" "%b" "%Y  -d '+1 day'`
convert_TodayPlus30=`date --date="$TodayPlus30" '+%s'`
convert_TodayPlus15=`date --date="$TodayPlus15" '+%s'`
convert_TodayPlus7=`date --date="$TodayPlus7" '+%s'`
convert_TodayPlus1=`date --date="$TodayPlus1" '+%s'`

function email {
(
        echo "To: mohamed.mansoor@example.com"
        echo "From: mohamed.mansoor@example.com"
        echo "Subject: [ALERT] [SSL EXPIRE] $certificate SSL Certificate Expires On $enddate"
        echo -e "Dear Team,"
        echo -e "\n\n$certificate SSL Certificate expires on $enddate. Kindly check and renew the certificate earliest."
        echo -e "\n\nScript Running in host: `hostname`"
        echo -e "\n\nScript Name: $0"
        echo -e "\n\nRegards,\nProd Support Team\n\n"
        echo -e "\n*** THIS IS A AUTO GENERATED ALERT GENERATED AT `date` ***\n"
)| /usr/sbin/sendmail -t -f mohamed.mansoor@example.com

}

for certificate in www.zebit.com:443
do
enddate=`openssl s_client -connect $certificate 2>/dev/null | openssl x509 -noout -enddate | awk -F= '{print $2}'| awk '{print $2" "$1" "$4}'`
convert_enddate=`date --date="$enddate" '+%s'`
if [ $convert_enddate -eq $convert_TodayPlus30 ]
then
email
elif [ $convert_enddate -eq $convert_TodayPlus15 ]
then
email
elif [ $convert_enddate -eq $convert_TodayPlus7 ]
then
email
elif [ $convert_enddate -eq $convert_TodayPlus1 ]
then
email
fi
done

No comments:

Post a Comment