Skip to content

Instantly share code, notes, and snippets.

@4ft35t
Forked from benkulbertis/cloudflare-update-record.sh
Last active December 4, 2022 15:55
Show Gist options
  • Save 4ft35t/510897486bc6986d19cac45b3b9ca1d0 to your computer and use it in GitHub Desktop.
Save 4ft35t/510897486bc6986d19cac45b3b9ca1d0 to your computer and use it in GitHub Desktop.

Revisions

  1. 4ft35t revised this gist Dec 4, 2022. 1 changed file with 51 additions and 51 deletions.
    102 changes: 51 additions & 51 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -1,55 +1,55 @@
    #!/bin/sh

    # CHANGE THESE
    auth_email="[email protected]"
    auth_key="c2547eb745079dac9320b638f5e225cf483cc5cfdda41" # found in cloudflare "profile" - "api-tokens" - "Global API Key"
    zone_name="example.com"
    record_name="www.example.com" # must add on cloudflare websit before run this script. Or you will get error `Method PUT not available for that URI`

    # MAYBE CHANGE THESE
    ip=$(curl -s http://ipv4.icanhazip.com)
    ip_file="ip.txt"
    id_file="cloudflare.ids"
    log_file="cloudflare.log"

    # LOGGER
    log() {
    if [ "$1" ]; then
    echo -e "[$(date)] - $1" >> $log_file
    fi
    ## edit this
    cf_ddns_host=myddns.mydomain.com
    # My Profile - API Tokens - Create Token - Use template of Edit zone DNS - Zone Resources - Include - All zones - Continue to summary - Create Token
    cf_token=
    ## edit end

    # auto fetch
    cf_zoneid=
    cf_recordid=

    servers='
    http://myip.ipip.net/s
    http://ipinfo.io/ip
    http://ipecho.net/plain
    http://members.3322.org/dyndns/getip
    http://ip.sb
    http://ifconfig.me
    '

    cfcurl(){
    curl -H "Authorization: Bearer $cf_token" -H "Content-Type: application/json" $@
    }
    # get root domain

    # SCRIPT START
    log "Check Initiated"

    if [ -f $ip_file ]; then
    old_ip=$(cat $ip_file)
    if [ $ip == $old_ip ]; then
    echo "IP has not changed."
    exit 0
    fi
    fi

    if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then
    zone_identifier=$(head -1 $id_file)
    record_identifier=$(tail -1 $id_file)
    else
    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | sed 's/,/\n/g' | awk -F'"' '/id/{print $6}' | head -1)
    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | sed 's/,/\n/g' | awk -F'"' '/id/{print $6}' | head -1)
    [ ! -z ${zone_identifier} ] && echo "$zone_identifier" > $id_file
    [ ! -z ${record_identifier} ] && echo "$record_identifier" >> $id_file
    fi

    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")

    if [[ -z ${update##*'"success":false'*} ]]; then
    message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
    log "$message"
    echo -e "$message"
    exit 1
    else
    message="IP changed to: $ip"
    echo "$ip" > $ip_file
    log "$message"
    echo "$message"
    fi
    get_cf_ids(){
    domain=$(curl "http://223.5.5.5/resolve?name=$cf_ddns_host&type=ns" | tr , '\n' | awk -F'"' '/Authority/{print $(NF-1)}')

    cf_zoneid=$(cfcurl "https://api.cloudflare.com/client/v4/zones?name=$domain" \
    | tr , '\n' \
    | awk -F'"' '/"id"/{print $(NF-1)}' \
    | head -1
    )

    cf_recordid=$(cfcurl -X GET "https://api.cloudflare.com/client/v4/zones/$cf_zoneid/dns_records?type=A&name=$cf_ddns_host" \
    | tr , '\n' \
    | awk -F'"' '/"id"/{print $(NF-1)}' \
    | head -1
    )

    sed -i "s/^cf_zoneid=.*$/cf_zoneid=$cf_zoneid/" $0
    sed -i "s/^cf_recordid=.*$/cf_recordid=$cf_recordid/" $0
    }

    for s in $servers
    do
    ip=$(curl -s -m3 $s)
    [ "$ip" ] && break
    done

    [ "$cf_recordid" ] || get_cf_ids

    cfcurl -X PUT "https://api.cloudflare.com/client/v4/zones/$cf_zoneid/dns_records/$cf_recordid" \
    --data '{"type":"A","name":"'$cf_ddns_host'","content":"'$ip'","ttl":3600,"proxied":false}'
  2. 4ft35t revised this gist Apr 14, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@

    # CHANGE THESE
    auth_email="[email protected]"
    auth_key="c2547eb745079dac9320b638f5e225cf483cc5cfdda41" # found in cloudflare account settings
    auth_key="c2547eb745079dac9320b638f5e225cf483cc5cfdda41" # found in cloudflare "profile" - "api-tokens" - "Global API Key"
    zone_name="example.com"
    record_name="www.example.com"
    record_name="www.example.com" # must add on cloudflare websit before run this script. Or you will get error `Method PUT not available for that URI`

    # MAYBE CHANGE THESE
    ip=$(curl -s http://ipv4.icanhazip.com)
  3. 4ft35t revised this gist Sep 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/bash
    #!/bin/sh

    # CHANGE THESE
    auth_email="[email protected]"
  4. 4ft35t revised this gist Sep 20, 2018. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -34,15 +34,15 @@ if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then
    zone_identifier=$(head -1 $id_file)
    record_identifier=$(tail -1 $id_file)
    else
    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
    echo "$zone_identifier" > $id_file
    echo "$record_identifier" >> $id_file
    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | sed 's/,/\n/g' | awk -F'"' '/id/{print $6}' | head -1)
    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | sed 's/,/\n/g' | awk -F'"' '/id/{print $6}' | head -1)
    [ ! -z ${zone_identifier} ] && echo "$zone_identifier" > $id_file
    [ ! -z ${record_identifier} ] && echo "$record_identifier" >> $id_file
    fi

    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")

    if [[ $update == *"\"success\":false"* ]]; then
    if [[ -z ${update##*'"success":false'*} ]]; then
    message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
    log "$message"
    echo -e "$message"
  5. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,7 @@ record_name="www.example.com"
    # MAYBE CHANGE THESE
    ip=$(curl -s http://ipv4.icanhazip.com)
    ip_file="ip.txt"
    id_file="cloudflare.ids"
    log_file="cloudflare.log"

    # LOGGER
    @@ -29,9 +30,15 @@ if [ -f $ip_file ]; then
    fi
    fi

    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )

    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
    if [ -f $id_file ] && [ $(wc -l $id_file | cut -d " " -f 1) == 2 ]; then
    zone_identifier=$(head -1 $id_file)
    record_identifier=$(tail -1 $id_file)
    else
    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')
    echo "$zone_identifier" > $id_file
    echo "$record_identifier" >> $id_file
    fi

    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")

  6. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -36,12 +36,13 @@ record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$
    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")

    if [[ $update == *"\"success\":false"* ]]; then
    log "API UPDATE FAILED. DUMPING RESULTS:\n$update"
    echo "API UPDATE FAILED. DUMPING RESULTS:"
    echo "$update"
    message="API UPDATE FAILED. DUMPING RESULTS:\n$update"
    log "$message"
    echo -e "$message"
    exit 1
    else
    message="IP changed to: $ip"
    echo "$ip" > $ip_file
    log "IP changed to: $ip"
    echo "IP changed to: $ip"
    log "$message"
    echo "$message"
    fi
  7. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ log_file="cloudflare.log"
    # LOGGER
    log() {
    if [ "$1" ]; then
    echo "[$(date)] - $1" >> $log_file
    echo -e "[$(date)] - $1" >> $log_file
    fi
    }

    @@ -36,8 +36,7 @@ record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$
    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")

    if [[ $update == *"\"success\":false"* ]]; then
    log "API UPDATE FAILED. DUMPING RESULTS:"
    log "$update"
    log "API UPDATE FAILED. DUMPING RESULTS:\n$update"
    echo "API UPDATE FAILED. DUMPING RESULTS:"
    echo "$update"
    exit 1
  8. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 17 additions and 10 deletions.
    27 changes: 17 additions & 10 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -11,15 +11,22 @@ ip=$(curl -s http://ipv4.icanhazip.com)
    ip_file="ip.txt"
    log_file="cloudflare.log"

    # LOGGER
    log() {
    if [ "$1" ]; then
    echo "[$(date)] - $1" >> $log_file
    fi
    }

    # SCRIPT START
    echo "[$(date)] - Check Initiated" >> $log_file
    log "Check Initiated"

    if [ -f $ip_file ]; then
    old_ip=$(cat $ip_file)
    if [ $ip == $old_ip ]; then
    echo "IP has not changed."
    exit 0
    fi
    old_ip=$(cat $ip_file)
    if [ $ip == $old_ip ]; then
    echo "IP has not changed."
    exit 0
    fi
    fi

    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )
    @@ -29,13 +36,13 @@ record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$
    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")

    if [[ $update == *"\"success\":false"* ]]; then
    echo "[$(date)] - API UPDATE FAILED. DUMPING RESULTS:" >> $log_file
    echo "$update" >> $log_file
    log "API UPDATE FAILED. DUMPING RESULTS:"
    log "$update"
    echo "API UPDATE FAILED. DUMPING RESULTS:"
    echo "$update"
    exit 1
    exit 1
    else
    echo "$ip" > $ip_file
    echo "[$(date)] - IP changed to: $ip" >> $log_file
    log "IP changed to: $ip"
    echo "IP changed to: $ip"
    fi
  9. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,7 @@ if [[ $update == *"\"success\":false"* ]]; then
    echo "$update" >> $log_file
    echo "API UPDATE FAILED. DUMPING RESULTS:"
    echo "$update"
    exit 1
    else
    echo "$ip" > $ip_file
    echo "[$(date)] - IP changed to: $ip" >> $log_file
  10. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -20,20 +20,21 @@ if [ -f $ip_file ]; then
    echo "IP has not changed."
    exit 0
    fi
    else
    echo "$ip" > $ip_file
    fi

    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )

    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')

    curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}" -o /dev/null
    update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}")

    if [ $? -eq 0 ]; then
    if [[ $update == *"\"success\":false"* ]]; then
    echo "[$(date)] - API UPDATE FAILED. DUMPING RESULTS:" >> $log_file
    echo "$update" >> $log_file
    echo "API UPDATE FAILED. DUMPING RESULTS:"
    echo "$update"
    else
    echo "$ip" > $ip_file
    echo "[$(date)] - IP changed to: $ip" >> $log_file
    echo "IP changed to: $ip"
    else
    echo "[$(date)] - API UPDATE FAILED" >> $log_file
    echo "API UPDATE FAILED."
    fi
  11. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,12 @@ auth_key="c2547eb745079dac9320b638f5e225cf483cc5cfdda41" # found in cloudflare a
    zone_name="example.com"
    record_name="www.example.com"

    # MAYBE CHANGE THESE
    ip=$(curl -s http://ipv4.icanhazip.com)
    ip_file="ip.txt"
    log_file="cloudflare.log"

    # SCRIPT START
    echo "[$(date)] - Check Initiated" >> $log_file

    if [ -f $ip_file ]; then
  12. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,15 @@
    #!/bin/bash

    # CHANGE THESE
    auth_email="[email protected]"
    auth_key="c2547eb745079dac9320b638f5e225cf483cc5cfdda41" # found in cloudflare account settings
    zone_name="example.com"
    record_name="www.example.com"

    ip=$(curl -s http://ipv4.icanhazip.com)
    ip_file="ip.txt"
    log_file="cloudflare.log"

    zone_identifier="ZONE IDENTIFIER"
    record_identifier="RECORD IDENTIFIER"
    record_name="RECORD NAME"

    echo "[$(date)] - Check Initiated" >> $log_file

    if [ -f $ip_file ]; then
    @@ -20,12 +22,16 @@ else
    echo "$ip" > $ip_file
    fi

    curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: [email protected]" -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" -H "Content-Type: application/json" --data "{\"id\":\":zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}" -o /dev/null
    zone_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$zone_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*' | head -1 )

    record_identifier=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" | grep -Po '(?<="id":")[^"]*')

    curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}" -o /dev/null

    if [ $? -eq 0 ]; then
    echo "[$(date)] - IP changed to: $ip" >> $log_file
    echo "IP changed to: $ip"
    else
    echo "[$(date)] - API UPDATE FAILED WTF SON" >> $log_file
    echo "API UPDATE FAILED WTF SON."
    echo "[$(date)] - API UPDATE FAILED" >> $log_file
    echo "API UPDATE FAILED."
    fi
  13. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ zone_identifier="ZONE IDENTIFIER"
    record_identifier="RECORD IDENTIFIER"
    record_name="RECORD NAME"

    echo "[$(date)] - Check Initiated" > $log_file
    echo "[$(date)] - Check Initiated" >> $log_file

    if [ -f $ip_file ]; then
    old_ip=$(cat $ip_file)
    @@ -23,9 +23,9 @@ fi
    curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: [email protected]" -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" -H "Content-Type: application/json" --data "{\"id\":\":zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}" -o /dev/null

    if [ $? -eq 0 ]; then
    echo "[$(date)] - IP changed to: $ip" > $log_file
    echo "[$(date)] - IP changed to: $ip" >> $log_file
    echo "IP changed to: $ip"
    else
    echo "[$(date)] - API UPDATE FAILED WTF SON" > $log_file
    echo "[$(date)] - API UPDATE FAILED WTF SON" >> $log_file
    echo "API UPDATE FAILED WTF SON."
    fi
  14. @benkulbertis benkulbertis revised this gist May 4, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/bin/bash

    ip_file="ip.txt"
    ip=$(curl -s http://ipv4.icanhazip.com)
    ip_file="ip.txt"
    log_file="cloudflare.log"

    zone_identifier="ZONE IDENTIFIER"
  15. @benkulbertis benkulbertis created this gist May 4, 2015.
    31 changes: 31 additions & 0 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/bin/bash

    ip_file="ip.txt"
    ip=$(curl -s http://ipv4.icanhazip.com)
    log_file="cloudflare.log"

    zone_identifier="ZONE IDENTIFIER"
    record_identifier="RECORD IDENTIFIER"
    record_name="RECORD NAME"

    echo "[$(date)] - Check Initiated" > $log_file

    if [ -f $ip_file ]; then
    old_ip=$(cat $ip_file)
    if [ $ip == $old_ip ]; then
    echo "IP has not changed."
    exit 0
    fi
    else
    echo "$ip" > $ip_file
    fi

    curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: [email protected]" -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" -H "Content-Type: application/json" --data "{\"id\":\":zone_identifier\",\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}" -o /dev/null

    if [ $? -eq 0 ]; then
    echo "[$(date)] - IP changed to: $ip" > $log_file
    echo "IP changed to: $ip"
    else
    echo "[$(date)] - API UPDATE FAILED WTF SON" > $log_file
    echo "API UPDATE FAILED WTF SON."
    fi