-
Notifications
You must be signed in to change notification settings - Fork 31
/
cf-add-a_record_proxied.sh
executable file
·61 lines (50 loc) · 2.01 KB
/
cf-add-a_record_proxied.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
# @author Fred Brooker <[email protected]>
CF_API="https://api.cloudflare.com/client/v4"
PAGE=1
PER_PAGE=50
cd "$(dirname "$0")"
export PATH=$PATH:/snap/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH=$PATH:$HOME/go/bin:$HOME/.cargo/bin:$HOME/bin:$HOME/scripts
. ./_includes.sh
if [ -f "./_cf_key.sh" ]; then . ./_cf_key.sh; fi
if [ -z "$CF_TOKEN" ]; then fail "Missing CF_TOKEN token value!"; fi;
# parameters
if [ $# -lt 3 ]; then info "\nUsage: \t$0 <all|domain.tld> <A_record> <value>\n\n"; exit; fi
if [ $# -eq 3 ]; then dom=$1; sub=$2; content=$3; fi
if [ $# -gt 3 ]; then info "\nUsage: \t$0 <all|domain.tld> <A_record> <value>\n\n"; exit; fi
# list all zones (max. 50)
r=$(curl -s -X GET "${CF_API}/zones?status=active&page=${PAGE}&per_page=${PER_PAGE}&match=all" \
-H "Authorization: Bearer ${CF_TOKEN}" -H "Content-Type: application/json");
names=$(echo $r|jq ".result[].name"|sed 's/"//g')
count=$(echo $names|sed 's/ /\n/g'|wc -l)
# multiple domains or single one
if [ "$dom" == "all" ]
then
yes_or_no "Process $count zone(s) and add \"$sub\" subdomain to all of them?" || exit 1
else
echo "Searching for \"$dom\" zone ..."
fi
j=0
for i in $names
do
((j++))
if [ "$dom" != "all" ]; then if [ "$dom" != "$i" ]; then continue; fi; fi
# get current zone
r=$(curl -s -X GET "${CF_API}/zones?name=${i}&status=active&match=all" \
-H "Authorization: Bearer ${CF_TOKEN}" -H "Content-Type: application/json")
id=$(echo $r|jq ".result[].id"|sed 's/"//g')
name=$(echo $r|jq ".result[].name"|sed 's/"//g')
# add A record
r=$(curl -s -X POST "${CF_API}/zones/${id}/dns_records" \
-H "Authorization: Bearer ${CF_TOKEN}" -H "Content-Type: application/json" \
--data '{"type":"A","name":"'${sub}'","content":"'${content}'","ttl":1,"proxied":true}')
result=$(echo $r|jq ".success")
# check result
if [ "$result" == "true" ]; then
infogreen "$j/$count ${name}"
else
infored "$j/$count ${name}"
echo $r|jq ".errors[].message"
fi
done