-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic-ip.sh
62 lines (53 loc) · 2.43 KB
/
dynamic-ip.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
#!/bin/bash
#You can start this script at startup by adding to crontab
DOMAIN="maindomain.com" # The domain name to update DNS for
INTERVAL=1 # Interval (in minutes) to check and update DNS
TIME="Modified: $(date +"%Y-%m-%d %H:%M")"
## CloudFlare Credentials
CF_API_KEY=""
CF_ZONE_ID=""
# Function to update DNS for a given host
update_dns() {
local name=$(echo "$1" | jq -r '.name')
local id=$(echo "$1" | jq -r '.id')
local dns_ip=$(echo "$1" | jq -r '.content') # Current DNS IP address
local proxied=$(echo "$1" | jq -r '.proxied')
local public_ip=$(dig @resolver4.opendns.com myip.opendns.com +short) # Public IP address
## --------------- CLOUDFLARE API CALL ------------------ ##
if [ "$public_ip" != "$dns_ip" ]; then # If public IP and DNS IP are different
# Update DNS using dynamic DNS service
cf_data="{\"content\": \"$public_ip\", \"proxied\": $proxied, \"type\": \"A\", \"comment\": \"$TIME\", \"id\": \"$id\", \"ttl\": 1}"
curl --request PATCH \
--url https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$id \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $CF_API_KEY" \
--data "$cf_data"
## --------------- ////////////////// ------------------ ##
fi
}
# Main loop to update DNS for all hosts
while true; do
filter=("@" "*") # Array of subdomains to filter from CloudFlare
# Append the DOMAIN to each value in the filter array
for ((i = 0; i < ${#filter[@]}; i++)); do
if [[ ${filter[$i]} == "@" ]]; then
filter[$i]=$DOMAIN
else
filter[$i]="${filter[$i]}.$DOMAIN"
fi
done
# Convert the array to a jq-readable format
jq_filter=$(printf '"%s",' "${filter[@]}" | sed 's/,$//')
# CURL to CloudFlare to get DNS Record ID
json_response=$(curl --request GET \
--url "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $CF_API_KEY")
# Parse the JSON response and store names, IDs, and IPs
cf_dns=$(echo "$json_response" | jq --argjson names "[$jq_filter]" '.result[] | select(.name as $n | $names | index($n) ) | select(.type == "A")')
# Iterate over the filtered results and pass each element to the function
echo "$cf_dns" | jq -c '.' | while IFS= read -r element; do
update_dns "$element"
done
sleep $(($INTERVAL * 60)) # Sleep for the specified interval (converted to seconds)
done