Unix: update a-record script

This commit is contained in:
Akemi Izuko 2024-11-06 12:04:08 -07:00
parent fbf1de5d86
commit 1a8ff1ba38
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC

View file

@ -206,16 +206,15 @@ domain! Of course, make sure your router's ports are forwarding to your
computer. computer.
We now need to make your computer update Cloudflare's DNS, whenever the IP We now need to make your computer update Cloudflare's DNS, whenever the IP
changes. I use the script below to do this. Fill in the `HOST`, `DOMAIN`, changes. I use the script below to do this. Fill in the `HOST4`, `HOST6`,
`TOKEN`, `ZONE_ID`. The `TOKEN` is your Cloudflare application token: `TOKEN`, `ZONE_ID`. The `TOKEN` is your Cloudflare application token:
```bash ```bash
#!/usr/bin/env bash #!/usr/bin/env bash
declare wan_ip_record wan_ip cf_records host_record cf_host_ip cf_rec_id declare wan_ip_record wan_ip cf_records host_record cf_host_ip cf_rec_id
declare -r HOST4='mycomputer' declare -r HOST4='mycomputer.example.com'
declare -r HOST6='mycomputer6' declare -r HOST6='mycomputer6.example.com'
declare -r DOMAIN='example.com'
declare -r TOKEN='CLOUDFLARE_TOKEN_HERE' declare -r TOKEN='CLOUDFLARE_TOKEN_HERE'
declare -r ZONE_ID='CLOUDFLARE_ZONEID_HERE' declare -r ZONE_ID='CLOUDFLARE_ZONEID_HERE'
@ -223,17 +222,47 @@ utc_date() {
date -u +'%Y-%m-%d_%H-%M-%S_UTC' date -u +'%Y-%m-%d_%H-%M-%S_UTC'
} }
#╔─────────────────────────────────────────────────────────────────────────────╗ cf_update_ip() {
#│ Gετ WΛN IP | local -r my_ip="$1"
#╚─────────────────────────────────────────────────────────────────────────────╝ local -r cf_ip="$2"
# Comment out as needed. Many home networks don't have ipv6 support local -r cf_id="$3"
if ! wan_ipv4="$(curl -s https://api4.ipify.org)"; then local -r host="$4"
echo "Request for ipv4 WAN timed out" >&2
exit 1 if [[ -z "$my_ip" || "$my_ip" == null ]]; then
elif ! wan_ipv6="$(curl -s https://api6.ipify.org)"; then echo "Failed to find local WAN ip: $my_ip" >&2
echo "Request for ipv6 WAN timed out" >&2 return 1
exit 1 elif [[ -z "$cf_ip" || "$cf_ip" == null ]]; then
echo "Failed to find content of A record for $host" >&2
return 1
elif [[ -z "$cf_id" || "$cf_id" == null ]]; then
echo "Failed to find A record ID for $host" >&2
return 1
elif [[ "$my_ip" == "$cf_ip" ]]; then
echo "Cloudflare for $host is up to date @ $(utc_date)" >&2
else
echo "Updating Cloudflare's for $host from $cf_ip to $my_ip" >&2
patch_response="$(curl -s --request PATCH \
--url "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${cf_id}" \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $TOKEN" \
--data '{
"comment": "'"${host} @ $(utc_date)"'",
"content": "'"$my_ip"'",
"name": "'"$host"'",
"proxied": false,
"ttl": 1
}')"
if [[ "$(echo "$patch_response" | jq -r '.success')" == true ]]; then
echo "Update to $my_ip for $host succeeded @ $(utc_date)" >&2
else
echo "Failed to update $host. DUMP:"
echo "$patch_response"
return 1
fi fi
fi
}
#╔─────────────────────────────────────────────────────────────────────────────╗ #╔─────────────────────────────────────────────────────────────────────────────╗
#│ Gετ Λ rεcδrd δη Clδμdflαrε | #│ Gετ Λ rεcδrd δη Clδμdflαrε |
@ -248,64 +277,31 @@ then
fi fi
declare -r cf_ipv4_record="$(echo "$cf_records" | declare -r cf_ipv4_record="$(echo "$cf_records" |
jq '.result[] | select(.name == "'"${HOST4}.${DOMAIN}"'")')" jq '.result[] | select(.name == "'"$HOST4"'")')"
declare -r cf_ipv4_ip="$(echo "$cf_ipv4_record" | jq --raw-output '.content')" declare -r cf_ipv4_ip="$(echo "$cf_ipv4_record" | jq --raw-output '.content')"
declare -r cf_ipv4_id="$(echo "$cf_ipv4_record" | jq --raw-output '.id')" declare -r cf_ipv4_id="$(echo "$cf_ipv4_record" | jq --raw-output '.id')"
declare -r cf_ipv6_record="$(echo $cf_records | declare -r cf_ipv6_record="$(echo $cf_records |
jq --raw-output '.result[] | select(.name == "'"${HOST6}.${DOMAIN}"'")')" jq --raw-output '.result[] | select(.name == "'"$HOST6"'")')"
declare -r cf_ipv6_ip="$(echo "$cf_ipv6_record" | jq --raw-output '.content')" declare -r cf_ipv6_ip="$(echo "$cf_ipv6_record" | jq --raw-output '.content')"
declare -r cf_ipv6_id="$(echo "$cf_ipv6_record" | jq --raw-output '.id')" declare -r cf_ipv6_id="$(echo "$cf_ipv6_record" | jq --raw-output '.id')"
#╔─────────────────────────────────────────────────────────────────────────────╗ #╔─────────────────────────────────────────────────────────────────────────────╗
#│ Sετ Λ rεcδrd τδ cμrrεητ WΛN | #│ Sετ Λ rεcδrd τδ cμrrεητ WΛN |
#╚─────────────────────────────────────────────────────────────────────────────╝ #╚─────────────────────────────────────────────────────────────────────────────╝
cf_update_ip() { if wan_ipv4="$(curl -s https://api4.ipify.org)"; then
local -r my_ip="$1" echo "ipv4 record: $wan_ipv4"
local -r cf_ip="$2" cf_update_ip "$wan_ipv4" "$cf_ipv4_ip" "$cf_ipv4_id" "$HOST4"
local -r cf_id="$3"
local -r host="$4"
local -r domain="$5"
local -r url="${host}.${domain}"
if [[ -z "$my_ip" || "$my_ip" == null ]]; then
echo "Failed to find local WAN ip: $my_ip" >&2
return 1
elif [[ -z "$cf_ip" || "$cf_ip" == null ]]; then
echo "Failed to find content of A record for $url" >&2
return 1
elif [[ -z "$cf_id" || "$cf_id" == null ]]; then
echo "Failed to find A record ID for $url" >&2
return 1
elif [[ "$my_ip" == "$cf_ip" ]]; then
echo "Cloudflare for $url is up to date @ $(utc_date)" >&2
else else
echo "Updating Cloudflare's for $url from $cf_ip to $my_ip" >&2 echo "Hosts timed out on ipv4. Not updating ipv4 A record" >&2
fi
patch_response="$(curl -s --request PATCH \ if wan_ipv6="$(curl -s https://api6.ipify.org)"; then
--url "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/dns_records/${cf_id}" \ echo "ipv6 record: $wan_ipv6"
--header 'Content-Type: application/json' \ cf_update_ip "$wan_ipv6" "$cf_ipv6_ip" "$cf_ipv6_id" "$HOST6"
--header "Authorization: Bearer $TOKEN" \
--data '{
"comment": "'"${host} @ $(utc_date)"'",
"content": "'"$my_ip"'",
"name": "'"$url"'",
"proxied": false,
"ttl": 1
}')"
if [[ "$(echo "$patch_response" | jq -r '.success')" == true ]]; then
echo "Update to $my_ip for $url succeeded @ $(utc_date)" >&2
else else
echo "Failed to update $url. DUMP:" echo "Hosts timed out on ipv6. Not updating ipv6 AAAA record" >&2
echo "$patch_response"
exit 1
fi fi
fi
}
cf_update_ip "$wan_ipv4" "$cf_ipv4_ip" "$cf_ipv4_id" "$HOST4" "$DOMAIN"
cf_update_ip "$wan_ipv6" "$cf_ipv6_ip" "$cf_ipv6_id" "$HOST6" "$DOMAIN"
``` ```
Now we need a systemd-timer to run this script. I run it once every 15 minutes. Now we need a systemd-timer to run this script. I run it once every 15 minutes.