主要使用到了Linux中的curl对证书信息进行获取,利用python通过正则截取信息中需要的部分
【直接看下面代码,python3.6 、centos7 测试通过】
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import re
import subprocess
from optparse import OptionParser
def main(domain):
cmd='curl -lvs https://{}/'.format(domain)
sslinfo=subprocess.getstatusoutput(cmd)[1]
print('domain:',domain)
m=re.search('subject:(.*?)\n.*?start date:(.*?)\n.*?expire date:(.*?)\n.*?common name:(.*?)\n.*?issuer:(.*?)\n',sslinfo)
print('subject:',m.group(1))
print('start date:',m.group(2))
print('expire date:',m.group(3))
print('common name:',m.group(4))
print('issuer:',m.group(5))
print('*'*80)
pass
if __name__ == '__main__':
domains=[]
parser=OptionParser()
parser.set_usage('python sslinfoGather.py -d domain \n python sslinfoGather.py -f fileName')
parser.add_option('-d','--domain',dest='domainName',help=' put the domain name')
parser.add_option('-f','--file',dest='fileName',help=' put the fileName which cotains domains;one line one domain')
options,_=parser.parse_args()
if(options.domainName==None and options.fileName==None):
# parser.print_usage()
parser.print_help()
exit()
if options.domainName!=None:
domains.append(options.domainName)
if options.fileName!=None:
f=open(options.fileName)
for domain in f:
domains.append(domain)
if len(domains)>0:
# domains=['www.baidu.com','www.sina.cn','www.jd.com']
for domain in domains:
try:
main(domain)
except Exception as e:
print('use python3 please\nError info:',str(e))
exit()
else:
print('input wrong ,check again!!!')
测试结果1:从文件中读取域名信息
测试结果2:从命令行直接读取域名信息
各位如果有需要,可以修改脚本根据证书的起止时间判断证书是否即将过期