-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert-check.sh
50 lines (43 loc) · 1.03 KB
/
cert-check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/sh
set -eu
NL='
'
OUT=''
VAL=''
NAME=''
while getopts h:vn name; do
case $name in
h) HOST="${OPTARG}";;
v) VAL='1';;
n) NAME='1';;
?) exit 2;;
esac
done
OUT_ALL="$(
echo \
| openssl s_client -showcerts -servername "${HOST}" -connect "${HOST}":443 2>/dev/null \
| openssl x509 -inform pem -noout -text
)"
if [[ "${VAL}" == '1' ]]; then
OUT_VAL="$(
grep -E "Not (Before|After)" <<<"${OUT_ALL}" \
| sed 's/^\ *//g' \
| cut -d ':' -f 2-
)"
OUT_BEF="$( awk 'NR == 1' <<<"${OUT_VAL}" )"
OUT_AFT="$( awk 'NR == 2' <<<"${OUT_VAL}" )"
AFT_EPOCH=$( date +%s -d "${OUT_AFT}" )
NOW_EPOCH=$( date +%s )
SEC_UNTIL=$(( ${AFT_EPOCH} - ${NOW_EPOCH} ))
OUT_UNTIL="$(( ${SEC_UNTIL} / 86400 ))"
OUT="${OUT}${NL}Valid since:${OUT_BEF}${NL}Valid until:${OUT_AFT}${NL}Days left: ${OUT_UNTIL}"
fi
if [[ "${NAME}" == '1' ]]; then
OUT_NAME="$(
grep -E "Subject: CN=.*" <<<"${OUT_ALL}" \
| sed 's/^\ *//g' \
| cut -d '=' -f 2
)"
OUT="${OUT}${NL}CN: ${OUT_NAME}"
fi
printf "%s\n" "${OUT}"