This is regards checking SSL certificate expiry on a HP-UX box. No date -d available.
I have the following;
#!/bin/bash
# Exit script if program fails or an unset variable is used
set -eu
server="BLABLA"
port="443"
graceperiod_days="30"
# Get expiry date of SSL certificate, in format 'Jan 31 11:59:00 2018 GMT'
enddate="$(openssl s_client -connect "$server:$port" 2>/dev/null | openssl x509 -noout -enddate | sed -e 's#notAfter=##')"
# Get today's date in format DD-MM-YYYY
todaysdate="$(date "+%d-%m-%Y")"
echo "Today's date is $todaysdate"
# Convert $enddate to format DD-MM-YYYY
enddate_formatted=$(printf '%s\n' "$enddate" | awk '{printf "%02d-%02d-%04d\n",$2,(index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)+2)/3,$4}')
echo "Certificate expiry date is $enddate_formatted"
# Compare expiry date with today's date
if "$todaysdate" -ge "$("$enddate_formatted" - "$graceperiod_days")"
then echo "$todaysdate is greater than $enddate_formatted. SSL certificate has expired!"
elif "$todaysdate" -lt "$("$enddate_formatted" - "$graceperiod_days")"
then echo "$todaysdate is before $enddate_formatted. Everything is OK!"
else
echo "ERROR"; fi
As far as I can tell, this should work, however the output is;
Today's date is 29-08-2018
Certificate expiry date is 21-07-2018
./test[22]: 21-07-2018: not found.
./test[22]: 29-08-2018: not found.
./test[24]: 21-07-2018: not found.
./test[24]: 29-08-2018: not found.
ERROR
What's going wrong?