Last active
June 9, 2024 18:11
-
-
Save hatsuyuki280/4371a460f4e1ebf9cb27801a6767877a to your computer and use it in GitHub Desktop.
cf-ddns script but with systemd.timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Update ddns | |
After=network.target | |
[Service] | |
ExecStart=/bin/bash /opt/path/to/cf-ddns.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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