您好,登錄后才能下訂單哦!
今天分享一個(gè)使用shell腳本實(shí)現(xià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è)地方:
n
變量免責(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)容。