Shell bản
demo.txt
[root@bogon aihuidi]# cat demo.txt
www.aihuidi.com:111.222.333.444
xxx.xxx.com:ip,ip
Cước bổn:
[root@localhost aihuidi]# vim check_ssl.sh
#!/bin/bash
forlinein$(catdemo.txt)
do
domain=$(echo${line}|awk-F':''{print $1}')
ip_pool=$(echo${line}|awk-F'[a-z]:''{print $2}'|sed's/\,/ /g')
# biến lịch IP trì
foripin${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}:4432>/dev/null|openssl x509-noout-dates)
if[[${text}]]
then
# chứng thư quá kỳ thời gian
end_date=$(echo"$text"|grep-i"notAfter"|awk-F'=''{print $2}')
# chuyển hoán thời gian trạc
end_timestamp=$(date-d"$end_date"+%s)
# đương tiền thời gian trạc
current_temestamp=$(date+%s)
# kế toán chứng thư đáo kỳ thặng dư thiên sổ
remain_date=$(((${end_timestamp}-${current_temestamp})/86400))
# như quả chứng thư quá kỳ thời gian giảm khứ đương tiền thời gian đích thiên sổ tiểu vu thất thiên đích thoại, tắc đề kỳ nhu yếu chuẩn bị canh hoán chứng thư liễu
if[[${remain_date}-lt7&&${remain_date}-ge0]]
then
echo-e"\e[31m thặng dư thời gian tiểu vu thất thiên! Thỉnh cập thời canh hoán chứng thư!\e[0m "
echo-e"\e[31mip:${ip},${domain}\e[0m "
elif[[${remain_date}-lt0]]
then
echo-e"\e[31m chứng thư dĩ quá kỳ! Thỉnh cập thời canh hoán chứng thư!\e[0m "
else
echo-e"\e[32m thặng dư thiên sổ vi:${remain_date}\e[0m "
fi
else
echo-e"\e[31mError!${ip}\e[0m "
echo-e"\e[31m${domain}\e[0m "
fi
done
done
# vận hành cước bổn
[root@localhost aihuidi]#./check_ssl.sh
Tham sổ giải thích
Kỳ trung notBefore thị khai thủy thời gian, notAfter thị quá kỳ thời gian
Python bản
importsocket
importssl
importdatetime
defget_certificate_expiry_date(domain):
try:
context=ssl.create_default_context()
withsocket.create_connection((domain,443))assock:
withcontext.wrap_socket(sock,server_hostname=domain)asssock:
cert=ssock.getpeercert()
expiry_date_str=cert['notAfter']
expiry_date=datetime.datetime.strptime(expiry_date_str,'%b %d %H:%M:%S %Y %Z')
returnexpiry_date
exceptExceptionase:
print("Error:",e)
returnNone
defmain():
domain="aihuidi.blog.csdn.net"# thế hoán vi nhĩ yếu kiểm trắc đích vực danh
expiry_date=get_certificate_expiry_date(domain)
ifexpiry_date:
current_date=datetime.datetime.now()
days_left=(expiry_date-current_date).days
print(f "SSL chứng thư đáo kỳ thời gian:{expiry_date}")
print(f "Thặng dư thiên sổ:{days_left}Thiên ")
if__name__=="__main__":
main()