基于python检查SSL证书到期情况代码实例
结合邮件告警和页面展示,再多的域名证书到期情况即可立马知道
代码示例:
#coding:utf-8
#查询域名证书到期情况
importre
importtime
importsubprocess
fromdatetimeimportdatetime
fromioimportStringIO
defmain(domain):
f=StringIO()
comm=f"curl-Ivshttps://{domain}--connect-timeout10"
result=subprocess.getstatusoutput(comm)
f.write(result[1])
m=re.search('startdate:(.*?)\n.*?expiredate:(.*?)\n.*?commonname:(.*?)\n.*?issuer:CN=(.*?)\n',f.getvalue(),re.S)
start_date=m.group(1)
expire_date=m.group(2)
common_name=m.group(3)
issuer=m.group(4)
#time字符串转时间数组
start_date=time.strptime(start_date,"%b%d%H:%M:%S%YGMT")
start_date_st=time.strftime("%Y-%m-%d%H:%M:%S",start_date)
#datetime字符串转时间数组
expire_date=datetime.strptime(expire_date,"%b%d%H:%M:%S%YGMT")
expire_date_st=datetime.strftime(expire_date,"%Y-%m-%d%H:%M:%S")
#剩余天数
remaining=(expire_date-datetime.now()).days
print('域名:',domain)
print('通用名:',common_name)
print('开始时间:',start_date_st)
print('到期时间:',expire_date_st)
print(f'剩余时间:{remaining}天')
print('颁发机构:',issuer)
print('*'*30)
time.sleep(0.5)
if__name__=="__main__":
domains=['www.baidu.com']
fordomainindomains:
main(domain)
结果示例:
域名:www.baidu.com
通用名:baidu.com
开始时间:2019-05-0901:22:02
到期时间:2020-06-2505:31:02
剩余时间:82天
颁发机构:GlobalSignOrganizationValidationCA-SHA256-G2,O=GlobalSignnv-sa,C=BE
******************************
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。