-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletsrenew.sh
More file actions
executable file
·74 lines (63 loc) · 1.96 KB
/
Copy pathletsrenew.sh
File metadata and controls
executable file
·74 lines (63 loc) · 1.96 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
if [[ $# == 0 ]]
then
printf "Usage: letsrenew.sh [-f <certFile>] [option]...\n\n"
printf " certfile: certificate file that shall be analyzed\n\n"
printf "Options:\n"
printf " -i <days>: if certificate rest validity is lower than <days> a info message is printed\n"
printf " -w <days>: if certificate rest validity is lower than <days> a warning message is printed\n"
printf " -r <days>: if certificate rest validity is lower than <days> 0 is returned 1 otherwise\n"
printf "\n\nExamples:\n"
printf " letsrenew.sh -f letsrenew.com.pem -r 10 && <letsencrypt-update.sh>\n"
printf " letsrenew.sh -f letsrenew.com.pem -r 10 && /opt/letsencrypt/letsencrypt-auto certonly --webroot -w /var/www/letsrenew.com/ -d letsrenew.com -d www.letsrenew.com --renew-by-default\n"
exit 1
fi
while [[ $# > 1 ]]
do
key="$1"
case $key in
-f)
certFile="$2"
shift # past argument
;;
-r)
certRenewalThreshold="$2"
shift # past argument
;;
-i)
certInfoThreshold="$2"
shift # past argument
;;
-w)
certWarningThreshold="$2"
shift # past argument
;;
*)
# unknown option
;;
esac
shift # past argument or value
done
if [ ! -f "$certFile" ]
then
printf "%-60s %-30s\n" "$certFile" "does not exist"
exit 1
fi
certSubject=$(openssl x509 -in "$certFile" -noout -subject | cut -d= -f 3)
certEnd=$(date --date="$(openssl x509 -in "$certFile" -enddate -noout | cut -d= -f 2)")
ms=$(($(date -ud "$certEnd" +%s)-$(date -u +%s)))
daysLeft=$(($ms/60/60/24))
if [ -n "$certWarningThreshold" ] && [ $daysLeft -lt $certWarningThreshold ]
then
echo "WARNING: $certFile $certSubject: $daysLeft days left!"
fi
if [ -n "$certInfoThreshold" ] && [ $daysLeft -lt $certInfoThreshold ]
then
printf "%-60s %-30s %-s\n" "$certFile" "$certSubject:" "$daysLeft days left."
fi
if [ -n "$certRenewalThreshold" ] && [ $daysLeft -lt $certRenewalThreshold ]
then
echo "$certFile $certSubject: $daysLeft days left. Renewal required."
exit 0
fi
exit 1