SSL 证书过期巡检脚本

哈喽大家好,我是咸鱼

我们知道 SSL 证书是会过期的,一旦过期之后需要重新申请。如果没有及时更换证书的话,就有可能导致网站出问题,给公司业务带来一定的影响

所以说我们要每隔一定时间去检查网站上的 SSL 证书是否过期

如果公司业务体量较大的话,肯定不止一个域名,而一个域名后面又会对应着多台机器,如果我们手动输入命令一台台检测的话,所需要的精力和时间是很大的

那么今天咸鱼跟大家介绍一个自己平常在用的自动检测 SSL 是否过期的 shell 脚本

思路

前面我们说到,一个公司(一个业务)底下可能会有多个域名多个 IP 地址,所以说我们需要整理出来放到一个文件里面,如下所示

#domain.txt
#域名:ip 池
www.baidu.com:180.101.50.242,180.101.50.188
www.bing.com:202.89.233.101,202.89.233.100
domain.txt
domain.txt
for line in $(cat domain.txt)
do
domain=$(echo ${line} | awk -F':' '{print $1}')
ip_pool=$(echo ${line} | awk -F '[a-z]:' '{print $2}' | sed 's/\,/ /g')
...
done
text
for line in $(cat domain.txt)
do
domain=$(echo ${line} | awk -F':' '{print $1}')
ip_pool=$(echo ${line} | awk -F '[a-z]:' '{print $2}' | sed 's/\,/ /g') # 遍历 ip 池
for ip in ${ip_pool}
do
echo -e "\e[33m---------------start to check---------------\e[0m"
echo -e "ip:${ip}\ndomain:${domain}" # 检测命令
text=$(echo | openssl s_client -servername ${domain} -connect ${ip}:443 2>/dev/null | openssl x509 -noout -dates -subject)
done
done

我们着重看下检测命令

echo | openssl s_client -servername ${domain} -connect ${ip}:443 2>/dev/null | openssl x509 -noout -dates -subject
echoopenssl s_client -servername www.baidu.com -connect 180.101.50.242:443openssl-servername-connect 2>/dev/null/dev/nullopenssl x509 -noout -datesopenssl x509-noout -dates
text
# echo | openssl s_client -servername www.baidu.com -connect 180.101.50.242:443 2>/dev/null | openssl x509 -noout -dates
notBefore=Jul 6 01:51:06 2023 GMT
notAfter=Aug 6 01:51:05 2024 GMT
notBeforenotAfter
text
if [[ ${text} ]] # text 里面有内容,不为空
then
do something
fi
notAfter
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 7 && ${remain_date} -ge 0 ]]
then
echo -e "\e[31m剩余时间小于七天!请及时更换证书!\e[0m"
echo -e "\e[31mip: ${ip}, ${domain}\e[0m"
elif [[ ${remain_date} -lt 0 ]]
then
echo -e "\e[31m证书已过期!请及时更换证书!\e[0m"
else
echo -e "\e[32m剩余天数为:${remain_date}\e[0m"
fi

我们来看下执行结果:

  • 证书未过期情况

  • 证书快过期

  • 证书已过期

完整脚本

for line in $(cat domain.txt)
do
domain=$(echo ${line} | awk -F':' '{print $1}')
ip_pool=$(echo ${line} | awk -F '[a-z]:' '{print $2}' | sed 's/\,/ /g')
for ip in ${ip_pool}
do
echo -e "\e[33m---------------start to check---------------\e[0m"
echo -e "ip:${ip}\ndomain:${domain}" text=$(echo | openssl s_client -servername ${domain} -connect ${ip}:443 2>/dev/null | openssl x509 -noout -dates )
# 判断命令是否执行成功,执行成功的话 text 变量里面是有内容的
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 7 && ${remain_date} -ge 0 ]]
then
echo -e "\e[31m剩余时间小于七天!请及时更换证书!\e[0m"
echo -e "\e[31mip: ${ip}, ${domain}\e[0m"
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[31mError!${ip}\e[0m"
echo -e "\e[31m${domain}\e[0m"
fi
done
done