This script will check if external IP is changed or not and will update the external IP of A or AAAA record in Cloudflare DNS using API token/ global API key method.
- Quick Tips:
- Location: Put the
cfddns.shfile to anyhwere you like. E.g.,/home/scripts. If SELinux is set to enforcing then copy the file tousr/local/bin. - Interface: Edit
interfaceto an active connection. You can find outinterfaceby simply runningip aorifconfig, if you find anything likeeth0,enp1s0,wlan0,wlp1s0etc. this is theinterface. Keep in mind that, you have to know which interface you want to use for this script. It will only work with an interface which can get a public IP.
- For Crontab:
- Open terminal.
- Give the file execute permission, type/ copy
sudo chmod +x /location/cfddns.shpressEnter. - Open crontab, type/ copy
crontab -epressEnter. - If you want to run the job every minute, type/ copy
* * * * * /location/cfddns.sh. - If you want to run the job every 5 minute, type/ copy
5 * * * * /location/cfddns.sh. - If you want more time flexibility then goto this link.
- After setting cron press
Escand type:wqthen pressEnter. - Setting up cron is completed!
- For Systemd Timer:
- Open terminal.
- Give the file execute permission, type/ copy
sudo chmod +x /location/cfddns.shpressEnter. - Create a systemd service unit, type/ copy
vi /etc/systemd/system/cfddns.servicepressEnter. - Copy all of the content from
cfddns.servicedown below. - After setting service unit press
Escand type:wqthen pressEnter. - Create a systemd timer unit at the same location of service unit, type/ copy
vi /etc/systemd/system/cfddns.timerpressEnter. - Copy all of the content from
cfddns.timerdown below. - If you want to run the timer unit every minute, edit/ copy
*:0/1(This is given on thecfddns.timerfile). - If you want to run the timer unit every 5 minute, edit/ copy
*:0/5. - If you want more time flexibility then goto this link.
- After setting timer unit press
Escand type:wqthen pressEnter. - Reload systemd, type/ copy
sudo systemctl daemon-reload. - Enable timer unit, type/ copy
sudo systemctl enable cfddns.timer. - Start timer unit, type/ copy
sudo systemctl start cfddns.timer. - Setting up systemd timer is completed!
This is a verbal representation of the script explaining how the script works.
-
Script will start executing and shows
[CF DDNS] IP CHECK INITIATED FOR example.com.... -
Now it will check for
INTERFACE CONNECTIVITYand shows[CF DDNS] CHECKING FOR INTERFACE CONNECTIVITY....
- If selected interface is disconnected physically then it will show
[CF DDNS] INTERFACE IS UNAVAILABLE!and exit. - If selected interface is connected physically but it is not active then it will show
[CF DDNS] INTERFACE IS DISCONNECTED!and exit. - If selected interface is connected physically and it is active then it will show
[CF DDNS] INTERFACE IS CONNECTED!and go to next step.
- Now it will check for
INTERNET AVAILABILITYand shows[CF DDNS] CHECKING FOR INTERNET AVAILABILITY....
- If selected interface has internet then it will show
[CF DDNS] INTERNET IS AVAILABLE!and go to next step. - If selected interface has no internet then it will show
[CF DDNS] INTERNET IS UNAVAILABLE!and exit.
- Now it will check for
SAVED IPand show[CF DDNS] GETTING SAVED IP....
- If you run this script for the first time then it will show
SAVED IP: NONE. - If you run this again then it will show
SAVED IP: 1.2.3.4.
- Now it will check for
CURRENT IPand show[CF DDNS] CHECKING FOR NEW IP....
- If selected interface public IP is unchanged and valid then it will show
[CF DDNS] NO NEW IP DETECTED!and exit. - If selected interface public IP is changed and valid then it will show
[CF DDNS] NEW IP DETECTED!and go to next step. - If selected interface can not check for
CURRENT IPdue to curl error/ IP is invalid then it will show[CF DDNS] CHECKING FOR NEW IP FAILED!and exit.
- Now it will check for
zone_id,record_id. if it get's new IP from Step 5 it will show[CF DDNS] CHECKING FOR ZONE & RECORD ID's....
- If
zone_idandrecord_idis found then it will show[CF DDNS] ZONE & RECORD ID'S FOUND!and go to next step. - If
zone_idandrecord_idis not found then it will show[CF DDNS] ZONE & RECORD ID'S NOT FOUND!,[CF DDNS] GETTING ZONE & RECORD ID'S.... After getting the id's it will show[CF DDNS] ZONE & RECORD ID'S SAVED!and go to next step.
- Now it will check for IP change. if it get's new IP from Step 5 it will show
[CF DDNS] UPDATING IP....
- If it failed to update the IP then it will show
[CF DDNS] IP UPDATE FAILED! DUMPING RESULTS: errormessage,[CF DDNS] SAVING LOG...and exit. - If it succeeded to update the IP then it will show
[CF DDNS] IP UPDATED TO: 1.2.3.4,[CF DDNS] SAVING IP...,[CF DDNS] SAVING LOG...and exit.
- Thanks to @benkulbertis and @lifehome!
- I have written the instructions based on CentOS 8.x.x.
- Files name are started with
0, 1, 2, 3because of orderly manner. - I am noob to this Scripting Business so if you find any mistakes please comment it below!
- Keep in mind that whether you use
crontaborsystemd timerboth will create huge size log files! It will be better if you uselogrotateto keep the log files at a minimum size. - The default
cfddns.timeris set to execute the script every minute. Please keep in mind not to spam the API or you will be rate limited. - A quote from Cloudflare Docs:
The global rate limit for the Cloudflare API is 1,200 requests per five (5) minute period per user, and applies cumulatively regardless of whether the request is made via the dashboard, API key, or API token.
If you exceed this limit, all API calls for the next five (5) minutes will be blocked, receiving a
HTTP 429 - Too Many Requestsresponse.