DDNS Cloudflare API


Because i move my dns service from Namecheap to Cloudflare, i need to update my dynamic isp ip one of my subdomain in cloudflare with ddns cloudflare api and here some script that i use :

THE SCRIPT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/sh
[ ! -f /var/tmp/current_ip.txt ] && touch /var/tmp/currentip.txt

zone_name="root_domain"
EMAIL="cloudflare_email"
AUTHKEY="cloudflare_api"

DNS1="first_domain"
DNS2="second_domain"

NEWIP=$(curl -s http://ipv4.icanhazip.com)
DOMAINIP1=$(dig "$DNS1" +short | awk '{ print ; exit }')
DOMAINIP2=$(dig "$DNS2" +short | awk '{ print ; exit }')
zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $AUTHKEY" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )

dns1_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$DNS1" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $AUTHKEY" -H "Content-Type: application/json" | | grep -Po '(?<="id":")[^"]*' | head -1)
dns2_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$DNS2" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $AUTHKEY" -H "Content-Type: application/json" | | grep -Po '(?<="id":")[^"]*' | head -1) if [ "$NEWIP" = "$DOMAINIP1" ] then echo "IP address unchanged" else curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$dns1_identifier" \ -H "X-Auth-Email: $EMAIL" \ -H "X-Auth-Key: $AUTHKEY" \ -H "Content-Type: application/json" \ --data "{"id":"$zone_identifier","type":"A","name":"$DNS1","content":"$NEWIP","proxied":true}" echo "$DNS1 IP address changed to $NEWIP from $DOMAINIP1" > /var/tmp/currentip.txt
fi

if [ "$NEWIP" = "$DOMAINIP2" ]
then
echo "IP address unchanged"
else
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$dns2_identifier" \
-H "X-Auth-Email: $EMAIL" \
-H "X-Auth-Key: $AUTHKEY" \
-H "Content-Type: application/json" \
--data "{"id":"$zone_identifier","type":"A","name":"$DNS2","content":"$NEWIP","proxied":true}"
echo "$DNS2 IP address changed to $NEWIP from $DOMAINIP2" >> /var/tmp/currentip.txt
fi

DON’T FORGET

don’t forget to set chmod +x to the file
add it to crontab

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.