zoukankan      html  css  js  c++  java
  • 检测域名证书有效期

    检测域名有效期

    #coding:utf-8
    
    import urllib2
    import requests
    import re
    
    import threading
    import datetime
    
    class Check_domain(object):
    
    	def __init__(self,url):
    		self.url=url
    		self.domain_url={}
    
    	def check_domain(self,bb):
    		header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36',
    		'Host':'whois.chinaz.com',
    		'Referer':'http://whois.chinaz.com/%s'%bb,
    		'Upgrade-Insecure-Requests':'1'
    		}
    
    		params={'domain':bb,'isforceupdate':'1','ws':''}
    		page_info=requests.get(self.url,headers=header,params=params)
    		# print page_info.url
    		result=page_info.content
    		reg = re.compile(r'<div class="fl WhLeList-left">过期时间</div><div class="fr WhLeList-right"><span>(.*?)</span>')
    		time_list=re.findall(reg,result)
    		if len(time_list):
    			end_time = datetime.datetime.strptime(str(time_list[0]),"%Y年%m月%d日")
    			time_list[0]=datetime.datetime.strftime(end_time,"%Y-%m-%d")
    			start_time =datetime.datetime.now()
    			# print (end_time-start_time).days
    			self.domain_url[bb]={'deadline':time_list[0],'dead_days':(end_time-start_time).days}
    
    if __name__ == '__main__':
    	thread=[]
    	check=Check_domain('http://whois.chinaz.com')
    	check_domain=['www.baidu.com','dding.cn','www.gmial.com']
    	for url in check_domain:
    		t= threading.Thread(target=check.check_domain,args=(url,))
    		t.start()
    		thread.append(t)
    	for t in thread:
    		t.join()
    	for s,v in check.domain_url.items():
    		print s,v
    
    

    发送邮箱

    def send_email(self,content, mailto, get_sub ):
    		mail_host = 'smtp.163.com' 
    		mail_user = '****@163.com'
    		mail_pwd = '*******'
    		msg = MIMEText( content.encode('utf8'), _subtype = 'html', _charset = 'utf8')  
    		msg['From'] = mail_user
    		msg['Subject'] = u'%s' %get_sub
    		msg['To'] = ",".join(mailto)
    		try:
    			print 'connecting ',mail_host  
    			smb=smtplib.SMTP(mail_host)
    			smb.login(mail_user, mail_pwd)
    			print 'send email'  
    			smb.sendmail(mail_user, ",".join(mailto), msg.as_string())  
    			print 'close the connection between the mail server'  
    			smb.close()
    		except Exception as e:  
    			print 'Exception: ', e
    
    

    test.sh

    #!/usr/bin/bash
    host=$1
    port=443
    end_date=`openssl s_client -host $host -port $port -showcerts </dev/null 2>/dev/null |
    	sed -n '/BEGIN CERTIFICATE/,/END CERT/p' |
          openssl x509 -text 2>/dev/null |
          sed -n 's/ *Not After : *//p'`
    if [ -n "$end_date" ]
    then
        end_date_seconds=`date '+%s' --date "$end_date"`
    # date指令format字符串时间。
        now_seconds=`date '+%s'`
        echo "($end_date_seconds-$now_seconds)/24/3600" | bc
    fi
    
    

    检测证书有效期

    #!/usr/bin/env python
    #coding:utf-8
    import os
    import smtplib  
    from email.mime.text import MIMEText  
    class Check_https(object):
    	def __init__(self,to_list,check_list):
    		for url in check_list:
    		     
    		    result = os.popen('sh test.sh {0}'.format(url))
    		    result = result.read().strip().split('
    ')[-1]
    		    print result
    		    if result.isdigit():
    			    if int(result)>0:
    			        print "沒有過期"
    			    else:
    				print "國了"
    			        self.send_email(result,to_list,'hi 过期了')
    
    	def send_email(self,content, mailto, get_sub ):
    		mail_host = 'smtp.163.com'  
    		mail_user = '*****@163.com'  
    		mail_pwd = '***********'
    		mail_postfix="163.com"
    		msg = MIMEText(content.encode('utf8'),_subtype='plain', _charset = 'utf8')  
    		msg['From'] = mail_user  
    		msg['Subject'] = u'%s' %get_sub  
    		msg['To'] = ",".join(mailto)  
    		try:  
    			print 'connecting ',mail_host  
    			smb=smtplib.SMTP()
    			smb.connect(mail_host)
    			smb.login(mail_user, mail_pwd)
    			print 'send email'  
    			smb.sendmail(mail_user, mailto, msg.as_string())  
    
    			print 'close the connection between the mail server'  
    			smb.close()  
    		except Exception as e:  
    			print 'Exception: ', e  
    
    if __name__ == '__main__':
        to_list = ['*****@163.com'] 
        check_list = ['www.baidu.com','dding.cn','www.gmail.com']
        check = Check_https(to_list,check_list)
    
    
  • 相关阅读:
    【openCV学习笔记】【2】读取并播放一段视频
    Net WebApi中使用Swagger
    .pfx格式和.Cer格式的区别
    数字证书原理
    ASP.NET发送电子邮件
    WindowsAzure上把WebApp和WebService同时部署在一个WebRole中
    JS原型与原型链终极详解
    三、ASP.NET MVC Controller 控制器(二:IController控制器的创建过程)
    二、ASP.NET MVC Controller 控制器(一:深入解析控制器运行原理)
    一、ASP.NET Routing路由(深入解析路由系统架构原理)
  • 原文地址:https://www.cnblogs.com/flyhgx/p/6850323.html
Copyright © 2011-2022 走看看