linux如何查看证书过期时间,shell脚本:通过域名获取证书的过期时间

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

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

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

参考博文:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值