This repository was archived by the owner on Jul 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdyndns.sh
More file actions
executable file
·65 lines (53 loc) · 2.08 KB
/
Copy pathdyndns.sh
File metadata and controls
executable file
·65 lines (53 loc) · 2.08 KB
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
#!/bin/sh
# Expected environment variables:
# API_TOKEN="<Cloudflare API Token>"
# HOSTED_ZONE_ID="<Cloudflare Zone ID>"
# RECORD_NAME="host.example.com"
TYPE="A"
COMMENT="Auto updated @ $(date)"
# get the current IP address
current_ip=$(curl --silent http://checkip.amazonaws.com/)
# validate the IP address
if [[ ! $current_ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
echo "IP address is invalid: $current_ip"
exit 1
fi
# find the current record in Cloudflare
get_record_response=$(curl --silent --request GET --url "https://api.cloudflare.com/client/v4/zones/${HOSTED_ZONE_ID}/dns_records" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer ${API_TOKEN}")
if [ "$(echo "$get_record_response" | jq -r '.success' )" == "false" ]; then
echo "Error getting record: $( echo "$get_record_response"| jq -r '.errors[0].message')"
exit 1
fi
current_record=$(echo "$get_record_response" | jq -r '.result[] | select (.name == "'"$RECORD_NAME"'") | select (.type == "'"$TYPE"'") | .id, .content')
if [ -z "$current_record" ]; then
echo "Record not found: $RECORD_NAME"
exit 1
fi
{ read record_id; read record_ip; } << EOF
${current_record}
EOF
if [ "$current_ip" == "$record_ip" ]; then
echo "The IP address $current_ip has not changed, exiting"
exit 0
fi
echo "The IP address has changed from $record_ip to $current_ip, updating record $RECORD_NAME"
update_record_response=$(curl --silent --request PUT --url "https://api.cloudflare.com/client/v4/zones/56de39c55b0fb37eac751a5f39f50760/dns_records/${record_id}" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer ${API_TOKEN}" \
--data "{
\"comment\": \"${COMMENT}\",
\"content\": \"${current_ip}\",
\"name\": \"$RECORD_NAME\",
\"proxied\": false,
\"type\": \"A\"
}")
if [ "$(echo "$get_record_response" | jq -r '.success' )" == "false" ]; then
echo "Error updating record: $( echo "$update_record_response"| jq -r '.errors[0].message')"
exit 1
else
echo "The record $RECORD_NAME has been successfully updated"
echo "$update_record_response" | jq
exit 0
fi