這里先說一下,CloudFlare是一家DNS供應商(我是這么理解的),他有提供很多服務(HTTPS/CDN等等),具體的大家可以自己去官網(wǎng)看一下:www.cloudflare.com
CloudFlare建議使用linux的curl命令使用開發(fā)的API
因shell不能很好的處理CloudFlare返回的json數(shù)據(jù),所以在使用shell的基礎上,增加了python腳本
本文件一共有四個腳本文件配合使用:
domians.txt :用來存放需要添加的域名
env.env :存放各種變量
filter_zone_id.py :用于將CloudFlare返回的數(shù)據(jù)過濾(過濾出域名及其區(qū)域ID)
filter_dns_id.py :用于將CloudFlare返回的數(shù)據(jù)過濾(過濾出解析記錄及其區(qū)域ID)
main.sh :存放linux命令,執(zhí)行時也是執(zhí)行這一個腳本即可
一、 domain.txt就不用介紹了,就是把頂級域名寫到里面,一行一個
env.env
登錄后復制
#!/bin/bash
#通用變量
#當前目錄
PWD=$(pwd)
PYTHON=$(which python)
ECHO=$(which echo)
#這里DNS指的是解析記錄
#賬號
CF_API_EMAIL=CloudFlare的登錄賬號
#秘鑰
CF_API_KEY=在用戶信息里面查看“Global KEY”
#組織名
organization_name="組織名"
#組織ID
organization_id="組織ID"
#設置CNAME 和 IP
CONTENT_CNAME=域名需要解析的別名“CNAME”
CONTENT_IP=域名需要解析的別名IP
#get_zone_id.cf -- 存放新增域名時Cloudflare返回的JSON 數(shù)據(jù)
#get_dns_id.cf -- 存放獲取DNS_ID時Cloudflare返回的JSON 數(shù)據(jù)
#filter_zone_id.py -- 用于從 "get_zone_id.cf" 中過濾出域名及其相對應的區(qū)域ID并寫入文件 "zone_id.cf"
#filter_dns_id.py -- 用于從 "get_dns_id.cf" 中過濾出DNS_ID及其DNS_NAME并寫入文件 "dns_id.cf"
二、main.sh
登錄后復制
#!/bin/bash
source ./env.env
#新增域名
for domain in $(cat $PWD/domains.txt)
do
curl -X POST -H "X-Auth-Key: ${CF_API_KEY}" \
-H "X-Auth-Email: ${CF_API_EMAIL}" \
-H "Content-Type: application/json" "https://api.cloudflare.com/client/v4/zones" \
--data '{"name":"'"${domain}"'","jump_start":true,"organization":{"name":"'"${organization_name}"'","id":"'"${organization_id}"'"}}' >> $PWD/get_zone_id.cf
${ECHO} -e "\n" >> $PWD/get_zone_id.cf
done
#執(zhí)行python腳本,過濾出域名的區(qū)域ID
$PYTHON $PWD/filter_zone_id.py
#獲取DNS ID列表
while read line
do
ZONE_NAME=$(${ECHO} "$line" | awk '{print $1}')
ZONE_ID=$(${ECHO} "$line" | awk '{print $2}')
curl -X GET "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" \
-H "X-Auth-Email: ${CF_API_EMAIL}" -H "X-Auth-Key: ${CF_API_KEY}" \
-H "Content-Type: application/json" >> $PWD/get_dns_id.cf
${ECHO} -e "\n" >> $PWD/get_dns_id.cf
done < $PWD/zone_id.cf
#執(zhí)行python腳本,過濾出DNS_ID
$PYTHON $PWD/filter_dns_id.py
#刪除DNS記錄
#刪除DNS記錄還需要指定區(qū)域ID,每個域名的區(qū)域DI不同,所以進行一個判斷,判斷DNS_NAME是否模糊匹配之前取到的ZONE_NAME
#如果匹配則使用這個ZONE_ID
while read zone
do
ZONE_NAME=$(${ECHO} "$zone" | awk '{print $1}')
ZONE_ID=$(${ECHO} "$zone" | awk '{print $2}')
while read dns
do
DNS_NAME=$(${ECHO} "$dns" | awk '{ print $1 }')
DNS_ID=$(${ECHO} "$dns" | awk '{ print $2 }')
if [[ "*$DNS_NAME" =—— "$ZONE_NAME" ]]
then
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${DNS_ID}" \
-H "X-Auth-Email:${CF_API_EMAIL}" \
-H "X-Auth-Key:${CF_API_KEY}" \
-H "Content-Type:application/json"
fi
done < $PWD/dns_id.cf
done < $PWD/zone_id.cf
#增加DNS記錄
#TTL=1 為自動
#proxied=true 使用CF的CDN,等于false是不使用
#data傳入變量格式: "'"$EVN"'"
#設置變量
while read line
do
ZONE_NAME=$(${ECHO} "$line" | awk '{print $1}')
ZONE_ID=$(${ECHO} "$line" | awk '{print $2}')
curl -X POST "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" \
-H "X-Auth-Email:${CF_API_EMAIL}" \
-H "X-Auth-Key:${CF_API_KEY}" \
-H "Content-Type:application/json" \
--data '{"type":"A","name":"@","content":"'"${CONTENT_IP}"'","ttl":1,"priority":10,"proxied":true}'
curl -X POST "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" \
-H "X-Auth-Email:${CF_API_EMAIL}" \
-H "X-Auth-Key:${CF_API_KEY}" \
-H "Content-Type:application/json" \
--data '{"type":"CNAME","name":"www","content":"'"${CONTENT_CNAME}"'","ttl":1,"priority":10,"proxied":false}'
curl -X POST "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records" \
-H "X-Auth-Email:${CF_API_EMAIL}" \
-H "X-Auth-Key:${CF_API_KEY}" \
-H "Content-Type:application/json" \
--data '{"type":"CNAME","name":"m","content":"'"${CONTENT_CNAME}"'","ttl":1,"priority":10,"proxied":false}'
done < $PWD/zone_id.cf
三、filter_zone_id.py
登錄后復制
#!/usr/bin/env python
#*- coding:utf-8 -*
#打開文件獲取添加域名成功后返回的數(shù)據(jù),然后獲取到域名的區(qū)域ID
#通過區(qū)域ID獲取DNS列表
import json
import os
import traceback
#traceback 完整輸出報錯信息
#str(e) 只給出異常信息,不包括異常信息的類型,如1/0的異常信息
#repr(e) 給出較全的異常信息,包括異常信息的類型,如1/0的異常信息
PWD = os.getcwd()
PWD_GET = PWD+'/get_zone_id.cf'
PWD_ZONE = PWD+'/zone_id.cf'
ERROR_FILE = PWD+'/zone_error.log'
#讀取添加域名時返回的字符串,并且循環(huán)寫入字典,以便于過濾出域名的區(qū)域ID
with open (PWD_GET,'r') as file:
i = 0
j = 0
dict = {}
list = []
for line in file:
if line.strip == " " or line == "\n" or line == " \n":
continue
try:
dict[i]=json.loads(line)
i+=1
except Exception as e:
j+=1
with open (ERROR_FILE,'a+') as file:
title = "----------\t\t第%d條報錯信息\t\t---------"%(j) + "\n"
#info = "報錯信息:" + traceback.format_exc() + "\n"
info = "報錯信息:" + repr(e) + "\n"
mation = "報錯行:" + line + "\n"
file.writelines(title)
file.writelines(info)
file.writelines(mation)
continue
#獲取所有的key,然后進行循環(huán)遍歷,獲取每個域名的區(qū)域ID
keys_list = dict.keys()
for key in keys_list:
ID=dict[key]['result']['id']
NAME=dict[key]['result']['name']
with open (PWD_ZONE,'a+') as file:
line = NAME + " " + ID + "\n"
file.writelines(line)
四、filter_dns_id.py
登錄后復制
#!/usr/bin/env python
#*- coding:utf-8 -*
#打開文件讀取cloudflare返回的DNS ID
import json
import os
PWD = os.getcwd()
PWD_GET = PWD+'/get_dns_id.cf'
PWD_DNS = PWD+'/dns_id.cf'
#讀取獲取DNS ID時返回的字符串,并且循環(huán)寫入字典,以便于過濾出DNS ID
with open (PWD_GET,'r') as file:
i = 0
dict = {}
for line in file:
if line.strip == " " or line == "\n" or line == " \n":
continue
dict[i]=json.loads(line)
i+=1
key_list = dict.keys()
for key in key_list:
list = dict[key]['result']
#一個域名可能有多個DNS記錄,cloudflare返回的是一個列表,列表中是一個一個的字典,
#一個key-values代表一個DNS記錄,所以這里需要遍歷列表中的字典
#通過取列表的索引值,循環(huán)每一個位置的字典
for i in list:
with open (PWD_DNS,'a+') as file:
line = i['name'] + " " + i['id'] + "\n"
file.writelines(line)