Skip to content

--- tags: - Shellcategories: - Linux - Networkdate:created: 2023-04-08# updated: 2024-01-19---

Cloudflare DDNS with ShellScripts

1. IntroductionWith the popularity of IPv6 and the unavailability of IPv4 on the public network, the demand for using IPv6 to access external networks has gradually increased. This article uses Cloudflare DNS API to implement IPv6 DDNS.

2. Environment Requirements- OS: Linux with SHELL(sh), Other Unix maybe.- HostName: example.com- HostName for DDNS: ddns.example.com

3. Steps to DDNS### (1) Obtain theZone IDandAPI Keyof the domain nameThe Zone ID can be obtained directly from the API column at the bottom right of the domain name control panel.

To obtain the API Key, you need to click "Get your API key" below the column, then enter the password and verification code to confirm. In fact, you can also create a new API Key yourself instead of using the global API Key.

(2) Add the AAAA record of the subdomain nameIn the "DNS" tab of the domain name panel, add an AAAA record in DNS Records, takingddns.example.comas an example:- Name: "ddns"- IPv6 address: "::1"- TTL: NULL- CDN: None

(3) Get the RecordID of the AAAA recordddns.example.comExecute on the command line:```

curl -s -X GET "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records?\ type=AAAA&name=DDNS_HOSTNAME&content=127.0.0.1&\ page=1&per_page=100&order=type&direction=desc&match=any" \ -H "X-Auth-Email: CF_EMAIL" \ -H "X-Auth-Key: CF_API_KEY" \ -H "Content-Type: application/json" \ | python -m json.tool `` - ZONE_ID isZone IDobtained in the first step- DDNS_HOSTNAME is the domain name for setting the AAAA record in the second step- CF_EMAIL is the registered email address of the Cloudflare account- CF_API_KEY isAPI KEYobtained in the first step After successful execution, a piece of output in JSON format will be returned. Find the"id"corresponding to the domain nameddns.example.cominresult`

(4)Create DDNS script

A useful Cloudflare Shell API is available on my gitlab: gitlab.dashdreams.com, all based on scripts above.

(5)Run the scriptAdd execution permissions to the scriptsudo chmod +x ddns.sh

Run scriptsudo ./ddns.sh

(6) Add scheduled operation- Use crontab- Using systemd timer (refer to this blog)Systemd-Timer

REF[1].https://zhuanlan.zhihu.com/p/69379645

[2].Execute-Scripts-on-Period-with-Systemd [3].Shell string interception [4].Linux-A brief analysis of text processing commands