Skip to content

Instantly share code, notes, and snippets.

@skys215
Created July 10, 2022 03:56
Show Gist options
  • Save skys215/97b18a1924605d28d7a51eb11f1d4f0c to your computer and use it in GitHub Desktop.
Save skys215/97b18a1924605d28d7a51eb11f1d4f0c to your computer and use it in GitHub Desktop.
Check SSL expiry and show notification
#!/bin/bash
# Domains to be checked
Domains=(example1.com example2.com)
# Port
Port="443"
# Show notification before n days before expiration
NoticeBeforeDays="30"
# Title of notification
TITLE="HTTP证书检查"
# Sound are in /System/Library/Sounds
SOUND="Frog"
for N in "${Domains[@]}" ; do
Domain=$N
# Get expiration date with Openssl tool
Cert_END_Time=$(echo | openssl s_client -servername ${Domain} -connect ${Domain}:${Port} 2> /dev/null | openssl x509 -noout -dates | grep 'After' | awk -F '=' '{print $2}' | awk '{print "-v" $1 "m -v" $2 "d -v" $4 "y"}')
# Covert date to timestamp
Cert_END_TimeStamp=$(date -j "$Cert_END_Time" '+%s')
# Get current timestamp
Create_TimeStamp=$(date +%s)
# Calculate how many days left to expiration date
Rest_Time=$(expr $(expr $Cert_END_TimeStamp '-' $Create_TimeStamp) / 86400)
Msg=''
# Set display message
if [ $Rest_Time -le 0 ]; then
Msg="❌ ${Domain} already expired!"
elif [ $Rest_Time -lt $NoticeBeforeDays ];then
Msg="⚠️ ${Domain} ${Rest_Time} days left until expire"
# else
# echo "✅ ${Domain}"
fi
osascript -e 'display notification "'"$Msg"'" with title "'"$TITLE"'" sound name "'"$SOUND"'"'
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment