python 获取域名到期时间的方法步骤
我要查询百度域名的到期时间或者开始时间
思路分析:如果在linux系统中直接使用下面命令即可:
echo | openssl s_client -servername www.baidu.com -connect www.baidu.com:443 2>/dev/null | openssl x509 -noout -dates|egrep ‘notAfter’|awk -F’=|GMT’ ‘{print $2}’
但是这个命令使用python2 的commands执行不成功,所以只能换成通过shell脚本去执行。
init_sh函数检查shell脚本不存在则创建,这样不需要多写一个脚本,有程序生成。
#!/usr/bin/python# -*- coding: utf-8 -*-# author: chentufeng# create time: 2020 12 25import commands,osscript_sh = '.tmp.sh'# 自动生成shell脚本用来执行shell命令获取时间def init_sh(): if not os.path.exists(script_sh): with open(script_sh, ’w’) as file_object: file_object.write('yuming=$1ntag=$2n' 'ymtime=`echo | openssl s_client -servername $yuming -connect $yuming:443 2>/dev/null | openssl x509 -noout -dates|egrep '$tag'|awk -F’=|GMT’ ’{print $2}’`n' #时间转换,如果需要也可以转换成其他格式 'date -d '$ymtime' ’+%Y-%m-%d %H:%M:%S’n')if __name__ == ’__main__’: #初始化函数 init_sh() yuming = 'www.baidu.com' tag = 'notBefore' #notBefore 开始时间;notAfter 到期时间 cmd = 'sh %s %s %s'%(script_sh, yuming, tag) restatus,retime = commands.getstatusoutput(cmd) print('获取的时间:%s'%retime)
输出结果:
到期时间[root@测试机 ~]# python aa.py获取的时间:2021-07-26 05:31:02开始时间[root@测试机 ~]# python aa.py获取的时间:2020-04-02 07:04:58
到此这篇关于python 获取域名到期时间的方法步骤的文章就介绍到这了,更多相关python 获取域名到期时间内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:
1. Python unittest生成测试报告过程解析2. python matplotlib工具栏源码探析二之添加、删除内置工具项的案例3. Django-simple-captcha验证码包使用方法详解4. CSS3实例分享之多重背景的实现(Multiple backgrounds)5. CSS Hack大全-教你如何区分出IE6-IE10、FireFox、Chrome、Opera6. php去掉数组的第一个值的两个函数:array_shift、array_splice7. 解决VUE项目使用Element-ui 下拉组件的验证失效问题8. 如何基于python3和Vue实现AES数据加密9. 用PHP读取和编写XML DOM10. Docker暴露2375端口导致服务器被攻击问题及解决方法

网公网安备