Skip to content

Instantly share code, notes, and snippets.

@greg-randall
Last active November 9, 2023 16:49
Show Gist options
  • Save greg-randall/f1a38e9af11e9988bcdadda348a4c248 to your computer and use it in GitHub Desktop.
Save greg-randall/f1a38e9af11e9988bcdadda348a4c248 to your computer and use it in GitHub Desktop.
Checks a list of sites for SSL expiration and texts you.
#!/bin/bash
#Reads a text file -- domains.txt -- containing one domain per line and checks to see if the ssl cert is expiring in the next 30 days
#Config varaiables,
#days is the number of days you'll get an alert for,
#phone number
#api key from https://textbelt.com/
days=30
phone="##########"
apikey="###"
# Doing this just to make sure the file is in unix format
dos2unix -n domains.txt temporary_unix_ended_file.txt > /dev/null 2>&1
# Check if the last character of the file is a newline
if [ "$(tail -c1 temporary_unix_ended_file.txt)" != "" ]
then
# If not, append a newline
echo "" >> temporary_unix_ended_file.txt
fi
domains=""
while read domain; do
expiry_date=$( echo | openssl s_client -servername $domain -connect $domain:443 2>/dev/null | openssl x509 -noout -enddate | cut -d "=" -f 2 )
expiry_date_seconds=$(date -d "$expiry_date" +%s)
current_date_seconds=$(date +%s)
# Calculate the difference in seconds
diff_seconds=$(( $expiry_date_seconds - $current_date_seconds ))
echo "$domain - $expiry_date"
# If the difference is less than 30 days
if [ -n "$diff_seconds" ] && [ $diff_seconds -lt $(($days*24*60*60)) ]; then
echo " Warning: SSL certificate for $domain expires in less than $days days!"
domains="$domains$domain, "
fi
done <temporary_unix_ended_file.txt
rm temporary_unix_ended_file.txt
if [ -n "$domains" ]; then
curl -X POST https://textbelt.com/text --data-urlencode phone=$phone --data-urlencode message="SSL Warning! $domains expire in under $days days." -d key=$apikey
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment