基于python檢查SSL證書(shū)到期情況代碼實(shí)例
結(jié)合郵件告警和頁(yè)面展示,再多的域名證書(shū)到期情況即可立馬知道
代碼示例:
# coding: utf-8 # 查詢(xún)域名證書(shū)到期情況import reimport timeimport subprocessfrom datetime import datetimefrom io import StringIOdef main(domain): f = StringIO() comm = f'curl -Ivs https://{domain} --connect-timeout 10' result = subprocess.getstatusoutput(comm) f.write(result[1]) m = re.search(’start date: (.*?)n.*?expire date: (.*?)n.*?common name: (.*?)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 字符串轉(zhuǎn)時(shí)間數(shù)組 start_date = time.strptime(start_date, '%b %d %H:%M:%S %Y GMT') start_date_st = time.strftime('%Y-%m-%d %H:%M:%S', start_date) # datetime 字符串轉(zhuǎn)時(shí)間數(shù)組 expire_date = datetime.strptime(expire_date, '%b %d %H:%M:%S %Y GMT') expire_date_st = datetime.strftime(expire_date,'%Y-%m-%d %H:%M:%S') # 剩余天數(shù) remaining = (expire_date-datetime.now()).days print (’域名:’, domain) print (’通用名:’, common_name) print (’開(kāi)始時(shí)間:’, start_date_st) print (’到期時(shí)間:’, expire_date_st) print (f’剩余時(shí)間: {remaining}天’) print (’頒發(fā)機(jī)構(gòu):’, issuer) print (’*’*30) time.sleep(0.5)if __name__ == '__main__': domains = [’www.baidu.com’] for domain in domains: main(domain)
結(jié)果示例:
域名: www.baidu.com通用名: baidu.com開(kāi)始時(shí)間: 2019-05-09 01:22:02到期時(shí)間: 2020-06-25 05:31:02剩余時(shí)間: 82天頒發(fā)機(jī)構(gòu): GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE******************************
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)3. React優(yōu)雅的封裝SvgIcon組件示例4. jsp文件下載功能實(shí)現(xiàn)代碼5. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法6. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲7. ASP基礎(chǔ)知識(shí)Command對(duì)象講解8. XML入門(mén)精解之結(jié)構(gòu)與語(yǔ)法9. ASP腳本組件實(shí)現(xiàn)服務(wù)器重啟10. jsp+mysql實(shí)現(xiàn)網(wǎng)頁(yè)的分頁(yè)查詢(xún)
