一、配置QQ邮箱
1、在浏览器登录我们的QQ邮箱,找到设置选择账户。
2、然后下来找到如下图所示界面,将POP3服务开启,开启时需要向腾讯发送一条短信进行认证。
3、开启服务后记录授权码,后续使用
二、安装邮件客户端工具 mailx
安装mailx服务:
yum -y install mailx
编辑vim /etc/.mailrc文件内容添加如下
set from=发件人邮箱@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=邮箱用户名
set smtp-auth-password=QQ邮箱里获取的授权密码
set smtp-auth=login
三、shell检测证书到期时间及邮件
Touch domain.txt文件内存放域名信息如:
Detection.sh(shell脚本内容):
#!/bin/bash
while :
do
for line in $(cat domain.txt)
do
domain=$(echo ${line} | awk -F':' '{print $1}')
for do in ${domain}
do
echo -e "\e[33m---------------start to check---------------\e[0m"
echo -e "域名:${domain}"
text=$(echo | openssl s_client -servername ${domain} -connect $domain:443 2>/dev/null | openssl x509 -noout -dates )
if [[ ${text} ]]
then
end_date=$(echo "$text" | grep -i "notAfter" | awk -F '=' '{print $2}')
end_timestamp=$(date -d "$end_date" +%s) # 转换成时间戳
current_timestamp=$(date +%s) # 当前时间戳
remain_date=$(( (${end_timestamp} - ${current_timestamp}) / 86400 ))
if [[ ${remain_date} -lt 30 && ${remain_date} -ge 0 ]]
then
echo -e "\e[32m剩余天数为:${remain_date}\e[0m"
echo -e "\e[31m剩余时间小于30天!请及时更换证书!\e[0m"
echo "$domain 网站的 SSL 证书还有 30 天后到期" |mail -s "测试服务器" ###@qq.com
echo "$domain 网站的 SSL 证书还有 30 天后到期" |mail -s "测试服务器" ###@qq.com
sleep 10
elif [[ ${remain_date} -lt 0 ]]
then
echo -e "\e[31m证书已过期!请及时更换证书!\e[0m"
else
echo -e "\e[32m剩余天数为:${remain_date}\e[0m"
fi
else
echo -e "\e[31m${domain}\e[0m"
fi
done
done
done
脚本执行命令(后台运行):
nohup sh Detection.sh > /dev/null 2>&1 &
四、配置阀值为200天查看报警信息及邮箱提醒