温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

域名有效期监控的最佳方案

发布时间:2020-03-05 00:36:54 来源:网络 阅读:348 作者:Marionxue 栏目:系统运维

今天分享一个使用shell脚本实现域名有效期的监控

域名有效期监控的最佳方案
不喜欢开场白,还是直接上干货...

#!/bin/bash
#检测域名是否过期
#作者:xuexiaobai@shell.com
#日期:20200224
#版本:v0.1

#当前日期时间戳,用于和域名的到期时间做比较
currentTimestamp=`date +%s`

#检测whois命令是否存在,不存在则安装whois包
isInstallWhois()
{
    which whois >/dev/null 2>/dev/null
    if [ $? -ne 0 ]
    then
        yum install -y whois || apt-get install whois -y
    fi
}

notify()
{
    expiredate=`whois $1 |grep 'Registry Expiry Date' |awk '{print $4}' |cut -d 'T' -f 1`
    #上面的$1代表域名,遍历循环出来的。
    #如果e_d的值为空,则过滤关键词'Expiration Time'
    if [ -z "$expiredate" ]
    then
        expiredate=`whois $1|grep 'Expiration Time' |awk '{print $3}'`

    fi
    #将域名过期的日期转化为时间戳
    expiredatestamp=`date -d $expiredate +%s`
    #计算半个月一共有多少秒
    # 15d 1296000  30d 2592000 35d 3024000 40d 3456000
    n=2592000
    timeBeforce=$[$expiredatestamp - $n] #过期时间15d以前的时间戳
    timeAfter=$[$expiredatestamp + $n] #过期时间15d以后的时间戳
    if [ $currentTimestamp -ge $timeBeforce ] && [ $currentTimestamp -lt $expiredatestamp ]
    then
        curl -X POST \
            -H 'Content-type: application/json' \
            --data '{"text":":warning:Domain '$1' will to be expired less then 15d. And domain '$1' expire date is '$expiredate' @xuexiaobai"}' \
            https://hooks.slack.com/services/*****/xxxxxxx/qqqqqqqqqqqqqqqqqqqqqq  
    fi
    if [ $currentTimestamp -ge $expiredatestamp ] 
    then
        curl -X POST \
            -H 'Content-type: application/json' \
            --data '{
                "text":":interrobang:Domain '$1' has been expired. And domain '$1' expire date is '$expiredate' @xuexiaobai"}' \
            https://hooks.slack.com/services/*****/xxxxxxx/qqqqqqqqqqqqqqqqqqqqqq
    fi
}

#检测上次运行的whois查询进程是否存在
#若存在,需要杀死进程,以免影响本次脚本执行
if pgrep whois &>/dev/null
then
    killall -9 whois
fi

isInstallWhois

for d in baidu.com google.com
do
  notify $d
done

以上脚本需要注意几个地方:

  1. 脚本中使用的是slack通知方式,如果你选择使用slack,需要修改一下hooks地址
  2. 可以自定义控制检查的有效期时长,控制是还有多少天过期进行通知,修改shell脚本中的那个n变量
  3. 放在一个定时任务中运行就可以了.

域名有效期监控的最佳方案

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI