Skip to content

Instantly share code, notes, and snippets.

@sharl
Last active August 29, 2015 14:01
Show Gist options
  • Save sharl/3ad6ca9610533f03865c to your computer and use it in GitHub Desktop.
Save sharl/3ad6ca9610533f03865c to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# usage: $0 domain[:port] [alert days] [timeout]
# ex)
# $0 yahoo.com 365
#
server=${1//:*/}
port=${1/*:/}
test "$server" = "$port" && port=443
limit=${2-60} # default 60 days
timeout=${3-3} # default timeout 3 seconds
notafter=$(timeout $timeout openssl s_client -connect $server:$port < /dev/null 2> /dev/null | openssl x509 -enddate -noout 2> /dev/null | sed 's/notAfter=//')
if [ "$notafter" ]; then
s=$(( $(date --date "$notafter" +%s) - $(date +%s) ))
if [ $limit -ge $((s / 86400)) ]; then
d=$(date --date "$notafter" +"%Y/%m/%d %H:%M:%S %Z")
days=$((s/86400))
hours=$((s%86400/3600))
echo -n "https://$server SSL certificate expires after "
if [ $days -gt 0 ]; then
echo -n $days day
if [ $days -gt 1 ]; then
echo -n s
fi
fi
if [ $hours -gt 0 ]; then
if [ $days -gt 0 ]; then
echo -n " and "
fi
echo -n $hours hour
if [ $hours -gt 1 ]; then
echo -n s
fi
fi
echo " ($d)"
exit 1
fi
else
echo "https://$server is not supported SSL or timeout?"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment