#!/bin/bash
#
Date_time=`date +%Y-%m-%d-%H`
pr_domain="/root/domain/111.txt"
dingding_api="https://oapi.dingtalk.com/robot/send?access_token=589d4db1c0e2***9418e07d4a199833031637398ac6cb40c6299d13023ca9ec969d"
#dingding_api="https://oapi.dingtalk.com/robot/send?access_token=740b293ff82c***35de96bfb0e13495eb32e268340db4e7a99e64417b5c9f7e2a43"
cf_api="https://api.cloudflare.com/client/v4/zones/ba6a1d4408c00c2af***450e112fbb1f6d3/custom_hostnames?per_page=250"
cf_user="bob.huo@orderplus.com"
cf_api_key="f6f44711479c29202***f57959ed3060a8f3"
#cloud_www=`cat $pr_domain|grep hostname|awk '{print $2}'|awk -F '"' '{print $2}' >$Domain`
#pull domain from cloudflare
#/usr/bin/curl http://open-api.orderplus.com/site/cloud_domains > $pr_domain
curl -X GET $cf_api -H "X-Auth-Email: $cf_user" -H "X-Auth-Key: $cf_api_key" -H "Content-Type: application/json" >$pr_domain
cat $pr_domain|grep hostname|awk '{print $2}'|awk -F '"' '{print $2}' >/root/domain/cloud_domain.txt
#sed -i 's/\[//g' $pr_domain && sed -i 's/\]//g' $pr_domain && sed -i 's/"//g' $pr_domain && sed -i 's/,/\n/g' $pr_domain && cat $pr_domain > /root/domain/domain.txt
#排除掉已下架的域名
Bad_Domain_txt=$(</root/domain/bad.txt)
for i in $Bad_Domain_txt
do
sed -i "/$i/d" /root/domain/cloud_domain.txt
done
#检测ssl证书
#Domain=$(</root/domain/cloud_domain.txt)
mkdir -p /data/domain/$Date_time
#Domain_txt=$(</root/domain/domain.txt)
for d in `cat /root/domain/cloud_domain.txt`;do /usr/bin/curl -I https://$d >/data/domain/$Date_time/$d.txt;done
ls -l /data/domain/$Date_time|awk '{if($5==0) print $9}' >/data/domain/bad_domain.txt
sed -i 's/.txt//g' /data/domain/bad_domain.txt
sed -i ':a ; N;s/\n/ / ; t a ; ' /data/domain/bad_domain.txt && sed -i 's/ /,/g' /data/domain/bad_domain.txt
Bad=`cat /data/domain/bad_domain.txt`
if test -s /data/domain/bad_domain.txt;then
curl -H "Content-type: application/json" -X POST -d '{"msgtype": "text","text": {"content": "'证书错误域名:$Bad'"}, "at": {"atMobiles": ["'18500164535'"], "isAtAll": false}}' $dingding_api
else
# curl -H "Content-type: application/json" -X POST -d '{"msgtype": "text","text": {"content": "'证书OK'"}, "at": {"atMobiles": ["'18500164535'"], "isAtAll": false}}' $dingding_api
echo "$Date_time.OK" >> /root/domain/okok.txt
fi