Skip to content

Instantly share code, notes, and snippets.

@AvverbioPronome
Last active March 30, 2022 13:35
Show Gist options
  • Save AvverbioPronome/426190cb0aeabbc72a7616a89db74a90 to your computer and use it in GitHub Desktop.
Save AvverbioPronome/426190cb0aeabbc72a7616a89db74a90 to your computer and use it in GitHub Desktop.
#!/bin/sh
cert_expiry(){
# cert_expiry host port
openssl s_client -connect "${1}:${2}" -servername "${1}" 2>/dev/null </dev/null | \
openssl x509 -noout -dates | grep notAfter | cut -d= -f 2
}
difference(){
# difference date date
echo $(( $(date -d "$1" +%s ) - $(date -d "$2" +%s) ))
}
format(){
# format seconds
seconds="$1"
seconds_in_day=86400
seconds_in_hour=3600
seconds_in_minute=60
output=""
while [ "$seconds" -gt "0" ]; do
if [ "$seconds" -ge "$seconds_in_day" ]; then
days="$(( seconds / seconds_in_day ))"
output="${days} days; "
seconds="$(( seconds - seconds_in_day * days ))"
elif [ "$seconds" -ge "$seconds_in_hour" ]; then
hours="$(( seconds / seconds_in_hour ))"
output="${output}${hours} hours; "
seconds="$(( seconds - seconds_in_hour * hours ))"
elif [ "$seconds" -ge "$seconds_in_minute" ]; then
minutes="$(( seconds / seconds_in_minute ))"
output="${output}$minutes minutes; "
seconds="$(( seconds - seconds_in_minute * minutes))"
else
output="${output}$seconds seconds;"
seconds=0
fi
done
echo "$output"
}
check_cert(){
# check_cert host port
expiry="$(cert_expiry $1 $2)"
echo "$expiry"
format $(difference "$expiry" "now")
}
check_cert "$1" "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment