当前位置: 移动技术网 > 科技>操作系统>Linux > shell脚本:通过域名获取证书的过期时间

shell脚本:通过域名获取证书的过期时间

2018年09月30日  | 移动技术网科技  | 我要评论

 

  需要两个文件,一个用于存储域名信息,另一个是检测脚本

  注意:这两个文件是在一个目录下

 

domain_ssl.info 【存储域名信息】

1 [root@mini05 20180930]# cat domain_ssl.info 
2 # 检测百度域名
3 www.baidu.com:443

 

check_domain_time.sh 【检测脚本】

 1 [root@mini05 20180930]# cat check_domain_time.sh 
 2 #!/bin/bash
 3 ################ version info ##################
 4 # create date: 2018-09-29
 5 # author:      zhang
 6 # mail:        zhang@xxxx.com
 7 # version:     1.0
 8 # attention:   通过域名获取证书的过期时间
 9 ################################################
10 # v1.0.0 2018-09-29 脚本编写    张
11 #      1.通过域名获取证书的过期时间
12 ################################################
13 
14 # 加载环境变量
15 . /etc/profile
16 . ~/.bash_profile
17 . /etc/bashrc
18 
19 # 脚本所在目录即脚本名称
20 script_dir=$( cd "$( dirname "$0"  )" && pwd )
21 script_name=$(basename ${0})
22 
23 readfile="${script_dir}/domain_ssl.info"
24 grep -v '^#' ${readfile} | while read line;do #读取存储了需要监测的域名的文件
25     # echo "${line}"
26     get_domain=$(echo "${line}" | awk -f ':' '{print $1}')
27     get_port=$(echo "${line}" | awk -f ':' '{print $2}')
28 
29     # echo ${get_domain}
30     # echo "${get_port}"
31     # echo "======"
32 
33     end_time=$(echo | openssl s_client -servername ${get_domain}  -connect ${get_domain}:${get_port} 2>/dev/null | openssl x509 -noout -dates |grep 'after'| awk -f '=' '{print $2}'| awk -f ' +' '{print $1,$2,$4 }' )
34     #使用openssl获取域名的证书情况,然后获取其中的到期时间
35     end_time1=$(date +%s -d "$end_time") #将日期转化为时间戳
36     now_time=$(date +%s -d "$(date | awk -f ' +'  '{print $2,$3,$6}')") #将目前的日期也转化为时间戳
37 
38     rst=$(($(($end_time1-$now_time))/(60*60*24))) # 到期时间减去目前时间再转化为天数
39 
40     echo "${rst}"
41 done

 

执行结果

1 [root@mini05 20180930]# ./check_domain_time.sh 
2 238

 

 

参考博文:

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网