shell

SSL证书监测脚本

admin · 9月9日 · 2021年
#!/bin/bash
#-----------------------------------------------------------------------------------#
domian_list=("luckinserver.cn" "abdas.cn")
#此处替换为自己的值即可,具体自己查阅github,server酱,也可使用bark推送
WX_URL=#############################################################################
sendkey=#############################################################################
msg_type=text
#-----------------------------------------------------------------------------------#
for domian_name in ${domian_list[@]};do
echo "当前检测的域名:" $domian_name
#获取到期时间
end_time=$(echo | timeout 1 openssl s_client -servername $domian_name -connect $domian_name:443 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | awk -F '=' '{print $2}' )
#下面命令可以等效,domain.crt替换为本地证书路径即可(仅适用证书在本机上)
end_time=$(openssl x509 -in domain.crt -noout -dates)
([ $? != 0 ] || [[ $end_time == '' ]]) &&  exit 10
#将查到的日期转化为时间戳
end_times=`date -d "$end_time" +%s `
#将当前时间转化为时间戳
current_times=`date -d "$(date -u '+%b %d %T %Y GMT') " +%s `
let remaining_time=$end_times-$current_times
days=`expr $remaining_time / 86400`
echo "证书剩余天数: " $days

if [ $days -lt 10 ];then

/root/.acme.sh/acme.sh --issue --dns dns_ali -d luckinserver.cn -d *.luckinserver.cn
rm -f /etc/nginx/cert/luckinserver.cn*
cp  /root/.acme.sh/luckinserver.cn/luckinserver.cn.cer /etc/nginx/cert/luckinserver.cn.cer
cp  /root/.acme.sh/luckinserver.cn/luckinserver.cn.key /etc/nginx/cert/luckinserver.cn.key
systemctl restart nginx
msg="域名:$domian_name\\n证书剩余天数:$days\\n证书即将到期,已自动续期"
curl -X POST $WX_URL -H "Content-Type:application/json" -d '{"sendkey":"'$sendkey'","msg_type":"'$msg_type'","msg":"'$msg'"}'

else
msg="域名:$domian_name\\n证书剩余天数:$days"
#curl -X POST $WX_URL -H "Content-Type:application/json" -d '{"sendkey":"'$sendkey'","msg_type":"'$msg_type'","msg":"'$msg'"}'
fi 
done