溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

域名有效期監(jiān)控的最佳方案

發(fā)布時(shí)間:2020-03-05 00:36:54 來(lái)源:網(wǎng)絡(luò) 閱讀:371 作者:Marionxue 欄目:系統(tǒng)運(yùn)維

今天分享一個(gè)使用shell腳本實(shí)現(xiàn)域名有效期的監(jiān)控

域名有效期監(jiān)控的最佳方案
不喜歡開(kāi)場(chǎng)白,還是直接上干貨...

#!/bin/bash
#檢測(cè)域名是否過(guò)期
#作者:xuexiaobai@shell.com
#日期:20200224
#版本:v0.1

#當(dāng)前日期時(shí)間戳,用于和域名的到期時(shí)間做比較
currentTimestamp=`date +%s`

#檢測(cè)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代表域名,遍歷循環(huán)出來(lái)的。
    #如果e_d的值為空,則過(guò)濾關(guān)鍵詞'Expiration Time'
    if [ -z "$expiredate" ]
    then
        expiredate=`whois $1|grep 'Expiration Time' |awk '{print $3}'`

    fi
    #將域名過(guò)期的日期轉(zhuǎn)化為時(shí)間戳
    expiredatestamp=`date -d $expiredate +%s`
    #計(jì)算半個(gè)月一共有多少秒
    # 15d 1296000  30d 2592000 35d 3024000 40d 3456000
    n=2592000
    timeBeforce=$[$expiredatestamp - $n] #過(guò)期時(shí)間15d以前的時(shí)間戳
    timeAfter=$[$expiredatestamp + $n] #過(guò)期時(shí)間15d以后的時(shí)間戳
    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
}

#檢測(cè)上次運(yùn)行的whois查詢進(jìn)程是否存在
#若存在,需要?dú)⑺肋M(jìn)程,以免影響本次腳本執(zhí)行
if pgrep whois &>/dev/null
then
    killall -9 whois
fi

isInstallWhois

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

以上腳本需要注意幾個(gè)地方:

  1. 腳本中使用的是slack通知方式,如果你選擇使用slack,需要修改一下hooks地址
  2. 可以自定義控制檢查的有效期時(shí)長(zhǎng),控制是還有多少天過(guò)期進(jìn)行通知,修改shell腳本中的那個(gè)n變量
  3. 放在一個(gè)定時(shí)任務(wù)中運(yùn)行就可以了.

域名有效期監(jiān)控的最佳方案

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI