https证书到期提醒脚本

PS:阿里云服务器默认是把25端口禁止的,我们可以使用SSL SMTP发送邮件,端口为465
邮件配置参考
https://blog.csdn.net/guo1wu3shi4/article/details/92849519

(1)使用SSL加密的方式,需要 QQ 邮箱的 SSL 证书,所以还需要手动的获取QQ邮箱的证书。具体命令如下:
#创建一个存放证书的目录
mkdir -p /root/.certs/
#获取QQ 邮箱的 SSL 证书
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
#添加第一个证书到证书数据库中
certutil -A -n "GeoTrust SSL CA" -t "C,,"  -d  ~/.certs  -i  ~/.certs/qq.crt
#添加第二个证书到证书数据库中
certutil -A -n "GeoTrust Global CA" -t "C,,"  -d  ~/.certs  -i  ~/.certs/qq.crt
#列出指定的目录下的所有证书
certutil -L -d /root/.certs

为了避免出现提示“Error in certificate: Peer's certificate issuer is not recognized.”执行下面的命令
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ~/.certs -i ~/.certs/qq.crt

#配置mail.rc:
vim /etc/mail.rc
在末尾添加:
set bsdcompat
set ssl-verify=ignore
set from=446634431@qq.com
set nss-config-dir=/root/.certs
set smtp="smtps://smtp.qq.com:465"
set smtp-auth-user=446634431@qq.com
#这个是qq邮件获取的smtp密码
set smtp-auth-password=skwibouvcuscbhcg
set smtp-auth=login

#脚本内容:
#需要检查提醒的域名
https_host=("lx.zzylx.top" "zzylx.top" "db.zzylx.top" "me.zzylx.top")
#接收邮箱
receive_mail=446634431@qq.com

for host in ${https_host[*]}
do
    get_domain=$host
    get_port=443

    END_TIME=$(echo | openssl s_client -servername ${get_domain}  -connect ${get_domain}:${get_port} 
2>/dev/null | openssl x509 -noout -dates |grep 'After'| awk -F '=' '{print $2}'| awk -F ' +' '{print 
$1,$2,$4 }' )
    END_TIME1=$(date +%s -d "$END_TIME")
    NOW_TIME=$(date +%s -d "$(date "+%Y-%m-%d")")

    RST=$(($(($END_TIME1-$NOW_TIME))/(60*60*24)))

    echo $get_domain: "${RST}"
    if [ $RST -lt 10 ]; then
        #小于十天发送提醒   
        echo $host :"https 证书即将到期 到期日期:${END_TIME} 还剩 ${RST} 天" | mail -s "https证书到期>
提醒" $receive_mail &> /dev/null &
    fi      
done

#给脚本添加可执行权限:
chmod +x 脚本名.sh

#添加crontab任务:
0 0 * * * /bin/bash /data/task/httpsmonitor/monitor.sh &> /dev/null
# 脚本 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×