Skip to content

Instantly share code, notes, and snippets.

@ibrasho
Created May 1, 2023 06:37
Show Gist options
  • Select an option

  • Save ibrasho/f68ecfa68a7d9e0a4c228451fa29cf69 to your computer and use it in GitHub Desktop.

Select an option

Save ibrasho/f68ecfa68a7d9e0a4c228451fa29cf69 to your computer and use it in GitHub Desktop.
A script to check if a website certificate expires within 10 days
#!/bin/bash
# Replace "example.com" with the domain name of the website you want to check
website="example.com"
# Get the SSL/TLS certificate expiry date
expiry_date=$(openssl s_client -connect ${website}:443 </dev/null 2>/dev/null | openssl x509 -noout -enddate | grep -i "notAfter" | awk -F'=' '{print $2}')
# Convert the expiry date to a Unix timestamp
expiry_timestamp=$(date -d "$(echo ${expiry_date} | awk '{print $1" "$2" "$3" "$4" "$5" "$6}')" +%s)
# Get the current Unix timestamp
current_timestamp=$(date +%s)
# Calculate the difference in seconds between the two timestamps
difference=$((expiry_timestamp - current_timestamp))
# Check if the expiry date is within 10 days of the current date
if [ ${difference} -lt $((10 * 24 * 60 * 60)) ]; then
echo "The SSL/TLS certificate for ${website} expires on ${expiry_date} and is within 10 days from today."
else
echo "The SSL/TLS certificate for ${website} expires on ${expiry_date} and is not within 10 days from today."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment