java中的接口是类吗
1613
2022-10-24
CloudFlare API:批量添加域名并添加解析记录
小生博客: :用来存放需要添加的域名 env.env :存放各种变量 filter_zone_id.py :用于将CloudFlare返回的数据过滤(过滤出域名及其区域ID) filter_dns_id.py :用于将CloudFlare返回的数据过滤(过滤出解析记录及其区域ID) main.sh :存放linux命令,执行时也是执行这一个脚本即可
一、 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 数据 #get_dns_id.cf -- 存放获取DNS_ID时Cloudflare返回的JSON 数据 #filter_zone_id.py -- 用于从 "get_zone_id.cf" 中过滤出域名及其相对应的区域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" "\ --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 #执行python脚本,过滤出域名的区域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 "\ -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 #执行python脚本,过滤出DNS_ID $PYTHON $PWD/filter_dns_id.py #删除DNS记录 #删除DNS记录还需要指定区域ID,每个域名的区域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 "\ -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 "\ -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 "\ -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 "\ -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 -* #打开文件获取添加域名成功后返回的数据,然后获取到域名的区域ID #通过区域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' #读取添加域名时返回的字符串,并且循环写入字典,以便于过滤出域名的区域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,然后进行循环遍历,获取每个域名的区域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时返回的字符串,并且循环写入字典,以便于过滤出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记录,所以这里需要遍历列表中的字典 #通过取列表的索引值,循环每一个位置的字典 for i in list: with open (PWD_DNS,'a+') as file: line = i['name'] + " " + i['id'] + "\n" file.writelines(line)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
评论列表