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.sh
file to anyhwere you like. E.g.,/home/scripts
. If SELinux is set to enforcing then copy the file tousr/local/bin
. - Interface: Edit
interface
to an active connection. You can find outinterface
by simply runningip a
orifconfig
, if you find anything likeeth0
,enp1s0
,wlan0
,wlp1s0
etc. 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.sh
pressEnter
. - Open crontab, type/ copy
crontab -e
pressEnter
. - 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
Esc
and type:wq
then pressEnter
. - Setting up cron is completed!
- For Systemd Timer:
- Open terminal.
- Give the file execute permission, type/ copy
sudo chmod +x /location/cfddns.sh
pressEnter
. - Create a systemd service unit, type/ copy
vi /etc/systemd/system/cfddns.service
pressEnter
. - Copy all of the content from
cfddns.service
down below. - After setting service unit press
Esc
and type:wq
then pressEnter
. - Create a systemd timer unit at the same location of service unit, type/ copy
vi /etc/systemd/system/cfddns.timer
pressEnter
. - Copy all of the content from
cfddns.timer
down below. - If you want to run the timer unit every minute, edit/ copy
*:0/1
(This is given on thecfddns.timer
file). - 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
Esc
and type:wq
then 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 CONNECTIVITY
and 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 AVAILABILITY
and 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 IP
and 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 IP
and 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 IP
due 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_id
andrecord_id
is found then it will show[CF DDNS] ZONE & RECORD ID'S FOUND!
and go to next step. - If
zone_id
andrecord_id
is 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, 3
because 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
crontab
orsystemd timer
both will create huge size log files! It will be better if you uselogrotate
to keep the log files at a minimum size. - The default
cfddns.timer
is 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 FAQ:
All calls through the Cloudflare Client API are rate-limited to 1200 every 5 minutes.