Easy dynamic DNS updates using BASH on Linux

What is the name of the domain?

What is the error number?

No error. This is a tip.

What is the error message?

No error. This is a tip.

What is the issue you’re encountering

Easily do automatic dynamic DNs updates.

What steps have you taken to resolve the issue?

Wrote the following to a file and added it to my crontab:

ZONE_ID=‘****************’ # for your example.com
URL=“https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records
API_TOKEN=‘**************’’ # for your account

function dnsUpdate {
SELECT=$(curl -s -X GET “$URL”
-H “Content-Type: application/json”
-H “Authorization: Bearer $API_TOKEN”
| sed “s:{"id":\n{"id":g” | grep “"name":"$1.”)
RECORD_ID=$(echo $SELECT | sed ‘s/^{“id”:“//;s/”,“name”.//')
OLD_IP=$(echo $SELECT | sed 's/.
“content”:"([.0-9])./\1/’)

echo “$1 ($RECORD_ID) = $OLD_IP”

if [[ -n "$OLD_IP"  &&  -n "$2"  &&  "$OLD_IP" != "$2" ]] ; then
    curl -s -X PUT "$URL/$RECORD_ID"  \
    -H "Content-Type: application/json"  \
    -H "Authorization: Bearer $API_TOKEN" \
   --data "{\"type\":\"A\",\"name\":\"$1\",\"content\":\"$2\",\"ttl\":300}"
    echo -e "\n\n$1.example.com ($RECORD_ID) IP address changed from $OLD_IP to $2"
fi

}

dnsUpdate myhostname $(curl -s api.ipify.org)

What feature, service or problem is this related to?

DNS records

What are the steps to reproduce the issue?

Searched the Internet for a simple solution. Found none.

Here is an updated file, without the above file being reformatted and losing some characters:

ZONE_ID='*********'      # for your example.com
URL="https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
API_TOKEN='*********'    # for your account

function dnsUpdate {
    SELECT=$(curl -s -X GET "$URL"  \
                  -H "Content-Type: application/json"  \
                  -H "Authorization: Bearer $API_TOKEN"  \
                             | sed -r     's/(\{"id":)/\n\1/g'  \
             | grep "\"name\":\"$1\."  \
             | grep '"type":"A"')
    RECORD_ID=$(echo $SELECT | sed -r     's/^\{"id":"([^"]*).*/\1/')
    FULL_NAME=$(echo $SELECT | sed -r    's/.*"name":"([^"]*).*/\1/')
       OLD_IP=$(echo $SELECT | sed -r 's/.*"content":"([^"]*).*/\1/')
#   echo "$FULL_NAME ($RECORD_ID) = $OLD_IP"
    if [[ -n "$OLD_IP"  &&  -n "$2"  &&  "$OLD_IP" != "$2" ]] ; then
        curl -s -X PUT "$URL/$RECORD_ID"  \
             -H "Content-Type: application/json"  \
             -H "Authorization: Bearer $API_TOKEN" \
             --data "{\"type\":\"A\",\"name\":\"$1\",\"content\":\"$2\",\"ttl\":300}"
        echo -e "\n\n$FULL_NAME ($RECORD_ID) IP address changed from $OLD_IP to $2"
    fi
}

dnsUpdate  myhostname  $(curl -s api.ipify.org)

If you have another local computer that has a different path to the Internet (I have two such), you can add the following line to the above file:

dnsUpdate anotherhostname $(ssh otherlocalhost -q 'curl -s api.ipify.org')

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.