这是在HP-UX盒上检查SSL证书到期的问题。没有可用的日期-d。
我有以下内容;
#!/bin/bash
# Exit script if program fails or an unset variable is used
set -eu
server="BLABLA"
port="443"
graceperiod_days="30"
# Get expiry date of SSL certificate, in format 'Jan 31 11:59:00 2018 GMT'
enddate="$(openssl s_client -connect "$server:$port" 2>/dev/null | openssl x509 -noout -enddate | sed -e 's#notAfter=##')"
# Get today's date in format DD-MM-YYYY
todaysdate="$(date "+%d-%m-%Y")"
echo "Today's date is $todaysdate"
# Convert $enddate to format DD-MM-YYYY
enddate_formatted=$(printf '%s\n' "$enddate" | awk '{printf "%02d-%02d-%04d\n",$2,(index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)+2)/3,$4}')
echo "Certificate expiry date is $enddate_formatted"
# Compare expiry date with today's date
if "$todaysdate" -ge "$("$enddate_formatted" - "$graceperiod_days")"
then echo "$todaysdate is greater than $enddate_formatted. SSL certificate has expired!"
elif "$todaysdate" -lt "$("$enddate_formatted" - "$graceperiod_days")"
then echo "$todaysdate is before $enddate_formatted. Everything is OK!"
else
echo "ERROR"; fi
据我所知,这应该有效,但是输出是正确的;
Today's date is 29-08-2018
Certificate expiry date is 21-07-2018
./test[22]: 21-07-2018: not found.
./test[22]: 29-08-2018: not found.
./test[24]: 21-07-2018: not found.
./test[24]: 29-08-2018: not found.
ERROR
出了什么问题?
答案 0 :(得分:0)
首先,您需要一种日期,您可以使用以下格式进行算术运算:
todaysdate_seconds=$(date +%s --date "$todaysdate") # assuming GNU date
gp_seconds=$((graceperiod_days * 86400))
enddate_seconds=$(date +%s --date "$enddate_formatted")
第二,您的if
语句缺少可以检查其退出状态的命令。您所拥有的就是此类命令的参数。而是使用
if test "$todaysdate_seconds" -ge "$("$enddate_seconds" - "$gp_seconds")"; then
或更简单的bash
算术命令
if (( todaysdate_seconds >= enddate_seconds - gp_seconds )); then