Skip to content

Instantly share code, notes, and snippets.

@hatsuyuki280
Last active June 9, 2024 18:11
Show Gist options
  • Save hatsuyuki280/4371a460f4e1ebf9cb27801a6767877a to your computer and use it in GitHub Desktop.
Save hatsuyuki280/4371a460f4e1ebf9cb27801a6767877a to your computer and use it in GitHub Desktop.
cf-ddns script but with systemd.timer
[Unit]
Description=Update ddns
After=network.target
[Service]
ExecStart=/bin/bash /opt/path/to/cf-ddns.sh
#!/bin/bash
echo -n "$(date) "
target_Domain_name="{REPLACE_IT}"
CF_API_EMAIL="{REPLACE_IT}"
CF_API_KEY="{REPLACE_IT}"
CF_ZONE_ID="{REPLACE_IT}"
CF_RECORD_ID4=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?type=A&name=$target_Domain_name" \
-H "X-Auth-Email: $CF_API_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json" | jq -r '.result[0].id')
CF_RECORD_ID6=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?type=AAAA&name=$target_Domain_name" \
-H "X-Auth-Email: $CF_API_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json" | jq -r '.result[0].id')
CF_IP4=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$CF_RECORD_ID4" \
-H "X-Auth-Email: $CF_API_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json" | jq -r '.result.content')
CF_IP6=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$CF_RECORD_ID6" \
-H "X-Auth-Email: $CF_API_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json" | jq -r '.result.content')
REAL_IP4="$(curl -4s ip.sb)"
REAL_IP6="$(curl -6s ip.sb)"
[ -z "$REAL_IP4" ] && {
echo "You have no IPv4 now."
} || {
[ "$CF_IP4" != "null" ] && {
[ "$CF_IP4" != "$REAL_IP4" ] && {
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$CF_RECORD_ID" \
-H "X-Auth-Email: $CF_API_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"'$target_Domain_name'","content":"'$REAL_IP4'","ttl":1,"proxied":true}'
} || {
echo "Real_IPv4 is same like CF_IPv4"
}
} || {
echo "CF_IPv4 not in use"
}
}
[ -z "$REAL_IP6" ] && {
echo "You have no IPv6 now."
} || {
[ "$CF_IP6" != "null" ] && {
[ "$CF_IP6" != "$REAL_IP6" ] && {
curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$CF_RECORD_ID" \
-H "X-Auth-Email: $CF_API_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json" \
--data '{"type":"AAAA","name":"'$target_Domain_name'","content":"'$REAL_IP6'","ttl":1,"proxied":true}'
} || {
echo "Real_IPv6 is same like CF_IPv6"
}
} || {
echo "CF_IPv6 not in use"
}
}
[Unit]
Description=Update DNS record with Cloudflare
[Timer]
OnCalendar=*:0/5
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment