-
Notifications
You must be signed in to change notification settings - Fork 31
/
base.sh
executable file
·109 lines (95 loc) · 2.77 KB
/
base.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
#title :base.sh
#description :
#author :[email protected]
#date :2022-05-03
#==============================================================================
zoneId=''
recordName=''
apiKey=''
if [ -n "$BASH_SOURCE" ]; then
workDir=$(
cd $(dirname "$BASH_SOURCE")
pwd
)
else
workDir=$(
cd $(dirname $0)
pwd
)
fi
echo "workDir: $workDir"
cd $workDir
. "$workDir/config.conf"
checkConfValid() {
local isValid=true
if [ -z "$zoneId" ]; then
echo "zoneId invalid"
return 1
fi
if [ -z "$recordName" ]; then
echo "recordName invalid"
return 1
fi
if [ -z "$apiKey" ]; then
echo "apiKey invalid"
return 1
fi
}
getIpv4Address() {
# try and choose one that works on your machine
# curl -k -s "http://members.3322.org/dyndns/getip" | grep -E -o '([0-9]+\.){3}[0-9]+' | head -n1 | cut -d' ' -f1
curl -s https://api.ipify.org
}
getIpv6Address() {
# try and choose one that works on your machine
curl -s -6 https://ifconfig.co/ip
# curl https://api64.ipify.org
}
listRecord() {
local zoneId=$1
local recordName=$2
local apiKey=$3
local result=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records?name=$recordName" \
-H "Content-Type:application/json" \
-H "Authorization: Bearer $apiKey")
local resourceId=$(echo "$result" | grep -Po '(?<="id":")[^"]+')
local currentValue=$(echo "$result" | grep -Po '(?<="content":")[^"]+')
local successStat=$(echo "$result" | grep -Po '(?<="success":)[^,]+')
if [ "$successStat" != "true" ]; then
return 1
fi
printf '%s\n%s' "$resourceId" "$currentValue"
}
updateRecord() {
local zoneId=$1
local recordName=$2
local apiKey=$3
local resourceId=$4
local type=$5
local value=$6
local result=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records/$resourceId" \
-H "Authorization: Bearer $apiKey" \
-H "Content-Type: application/json" \
--data "{\"type\":\"$type\",\"name\":\"$recordName\",\"content\":\"$value\",\"ttl\":600,\"proxied\":false}")
local successStat=$(echo "$result" | grep -Po '(?<="success":)[^,]+')
[ "$successStat" = "true" ]
return $?
}
createRecord() {
local zoneId=$1
local recordName=$2
local apiKey=$3
local type=$4
local value=$5
local result=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$zoneId/dns_records" \
-H "Authorization: Bearer $apiKey" \
-H "Content-Type: application/json" \
--data "{\"type\":\"$type\",\"name\":\"$recordName\",\"content\":\"$value\",\"ttl\":600,\"proxied\":false}")
local successStat=$(echo "$result" | grep -Po '(?<="success":)[^,]+')
if [ "$successStat" != "true" ]; then
return 1
fi
local recordId=$(echo "$result" | grep -Po '(?<="id":")[^"]+')
echo "$recordId"
}