Skip to content

Instantly share code, notes, and snippets.

@Gondost
Forked from lifehome/README.md
Last active March 16, 2020 16:43

Revisions

  1. Gondost revised this gist Mar 16, 2020. 1 changed file with 61 additions and 39 deletions.
    100 changes: 61 additions & 39 deletions cfupdater-v4
    Original file line number Diff line number Diff line change
    @@ -1,47 +1,69 @@
    #!/bin/bash

    # Forked by benkulbertis/cloudflare-update-record.sh
    # CHANGE THESE
    auth_email="john.appleseed@example.org" # The email used to login 'https://dash.cloudflare.com'
    auth_key="f1nd7h47fuck1n6k3y1ncl0udfl4r3c0n50l3" # Top right corner, "My profile" > "Global API Key"
    zone_identifier="f1nd7h3fuck1n6z0n31d3n71f13r4l50" # Can be found in the "Overview" tab of your domain
    record_name="ipv4.example.org" # Which record you want to be synced
    declare -A EMAILKEY=(
    # Login email and Global API key
    # [auth_email]=auth_key
    [email@domain.com]=yourcloudflareapikey
    )

    declare -A RECORDEMAIL=(
    # [domain]=auth_email
    [sub.domain.com]=email@domain.com
    [domain.com]=email@domain.com
    )

    declare -A RECORDZONE=(
    # [record_name]=zone_identifier
    [sub.domain.com]=yourcloudflaredomainzoneid
    [domain.com]=yourcloudflaredomainzoneid
    )

    # DO NOT CHANGE LINES BELOW
    ip=$(curl -s https://ipv4.icanhazip.com/)

    # SCRIPT START
    echo "[Cloudflare DDNS] Check Initiated"

    # Seek for the record
    record=$(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")

    # Can't do anything without the record
    if [[ $record == *"\"count\":0"* ]]; then
    >&2 echo -e "[Cloudflare DDNS] Record does not exist, perhaps create one first?"
    exit 1
    fi

    # Set existing IP address from the fetched record
    old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)

    # Compare if they're the same
    if [ $ip == $old_ip ]; then
    echo "[Cloudflare DDNS] IP has not changed."
    exit 0
    fi

    # Set the record identifier from result
    record_identifier=$(echo "$record" | grep -Po '(?<="id":")[^"]*' | head -1)

    # The execution of update
    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\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip\"}")

    # The moment of truth
    case "$update" in
    *"\"success\":false"*)
    >&2 echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update"
    exit 1;;
    *)
    echo "[Cloudflare DDNS] IPv4 context '$ip4' has been synced to Cloudflare.";;
    esac

    for record_name in "${!RECORDZONE[@]}"
    do
    # Get all the required values from associative arrays
    zone_identifier=${RECORDZONE[$record_name]}
    auth_email=${RECORDEMAIL[$record_name]}
    auth_key=${EMAILKEY[$auth_email]}
    echo "[Cloudflare DDNS] Check Initiated for $record_name"

    # Seek for the record
    record=$(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")

    # Can't do anything without the record
    if [[ $record == *"\"count\":0"* ]]; then
    >&2 echo -e "[Cloudflare DDNS] Record does not exist, perhaps create one first?"
    continue
    fi

    # Set existing IP address from the fetched record
    old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)

    # Compare if they're the same
    if [ $ip == $old_ip ]; then
    echo "[Cloudflare DDNS] IP for $record_name has not changed."
    continue
    fi

    # Set the record identifier from result
    echo "[Cloudflare DDNS] Old IP was $old_ip, trying to set new IP $ip"
    record_identifier=$(echo "$record" | grep -Po '(?<="id":")[^"]*' | head -1)

    # The execution of update
    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\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip\"}")

    # The moment of truth
    case "$update" in
    *"\"success\":false"*)
    >&2 echo -e "[Cloudflare DDNS] Update failed for $record_identifier. IP is still $old_ip. DUMPING RESULTS:\n$update"
    continue;;
    *)
    echo "[Cloudflare DDNS] IPv4 context '$ip' for $record_name has been synced to Cloudflare.";;
    esac

    done
  2. @lifehome lifehome revised this gist Dec 15, 2018. 2 changed files with 5 additions and 5 deletions.
    5 changes: 4 additions & 1 deletion cfupdate.service
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,7 @@ After=network.target

    [Service]
    Type=oneshot
    ExecStart=/usr/local/bin/cfupdater
    ExecStart=/usr/local/bin/cfupdater

    [Install]
    WantedBy=multi-user.target
    5 changes: 1 addition & 4 deletions cfupdate.timer
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,4 @@
    Description=Run cfupdate.service every minute

    [Timer]
    OnCalendar=*:0/1

    [Install]
    WantedBy=multi-user.target
    OnCalendar=*:0/1
  3. @lifehome lifehome revised this gist Nov 20, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,8 @@ This is a bash script to act as a Cloudflare DDNS client, useful replacement for
    2) `chmod +x /usr/local/bin/cfupdater`
    3) Create a systemd service unit at `/etc/systemd/system/`, the `cfupdate.service` is shown as an example.
    4) Create a systemd timer unit at **the same location of the service unit**, the `cfupdate.timer` is shown as an example.
    5) `sudo systemctl enable cfupdate.timer`
    6) `sudo systemctl start cfupdate.timer`

    # Note
    The default `cfupdate.timer` is set to execute the script **every minute**.
  4. @lifehome lifehome revised this gist Nov 20, 2018. 2 changed files with 5 additions and 5 deletions.
    5 changes: 1 addition & 4 deletions cfupdate.service
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,4 @@ After=network.target

    [Service]
    Type=oneshot
    ExecStart=/usr/local/bin/cfupdater

    [Install]
    WantedBy=multi-user.target
    ExecStart=/usr/local/bin/cfupdater
    5 changes: 4 additions & 1 deletion cfupdate.timer
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,7 @@
    Description=Run cfupdate.service every minute

    [Timer]
    OnCalendar=*:0/1
    OnCalendar=*:0/1

    [Install]
    WantedBy=multi-user.target
  5. @lifehome lifehome revised this gist Sep 26, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions cfupdater-dualstack
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ record_name="ipv4.example.org" # Which record you want to be

    # DO NOT CHANGE LINES BELOW
    ip4=$(curl -s https://ipv4.icanhazip.com/)
    ip6=$(curl -s https://ipv46.icanhazip.com/)
    ip6=$(curl -s https://ipv6.icanhazip.com/)

    # SCRIPT START
    echo "[Cloudflare DDNS] Check Initiated"
    @@ -30,7 +30,7 @@ old_ip6=$(echo "$record6" | grep -Po '(?<="content":")[^"]*' | head -1)

    # Compare either one is the same
    # NOTE: The script will update even one IP remains the same.
    if [ $ip4 == $old_ip4 && $ip6 == $old_ip6 ]; then
    if [[ $ip4 == $old_ip4 && $ip6 == $old_ip6 ]]; then
    echo "[Cloudflare DDNS] IPs have not changed."
    exit 0
    fi
    @@ -40,8 +40,8 @@ record4_identifier=$(echo "$record4" | grep -Po '(?<="id":")[^"]*' | head -1)
    record6_identifier=$(echo "$record6" | grep -Po '(?<="id":")[^"]*' | head -1)

    # The execution of update
    update4=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier4" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip4\"}")
    update6=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier6" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip6\"}")
    update4=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record4_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip4\"}")
    update6=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record6_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip6\"}")

    # The moment of truth
    if [[ $update4 == *"\"success\":false"* || $update6 == *"\"success\":false"* ]]; then
  6. @lifehome lifehome revised this gist Jul 20, 2018. 4 changed files with 65 additions and 9 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@ This is a bash script to act as a Cloudflare DDNS client, useful replacement for
    1) Put the `cfupdater` files to `/usr/local/bin`
    - If you are using IPv4 for A record, append `-v4` to `cfupdater` in the following systemd service unit.
    - If you are using IPv6 for AAAA record, append `-v6` to `cfupdater` in the following systemd service unit.
    - If you prefer a **dual-stack** record, append `-dualstack` to `cfupdater` in the following systemd service unit.
    2) `chmod +x /usr/local/bin/cfupdater`
    3) Create a systemd service unit at `/etc/systemd/system/`, the `cfupdate.service` is shown as an example.
    4) Create a systemd timer unit at **the same location of the service unit**, the `cfupdate.timer` is shown as an example.
    @@ -13,6 +14,9 @@ This is a bash script to act as a Cloudflare DDNS client, useful replacement for
    The default `cfupdate.timer` is set to execute the script **every minute**.
    Please keep in mind not to spam the API or you will be rate limited.

    The dual-stack script has NOT been tested, use with caution.
    The dual-stack script will always sync upon either IPv4 or IPv6 has changed.

    A quote from Cloudflare FAQ:
    > All calls through the Cloudflare Client API are rate-limited to 1200 every 5 minutes.
    >
    52 changes: 52 additions & 0 deletions cfupdater-dualstack
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    #!/bin/bash

    # Forked by benkulbertis/cloudflare-update-record.sh
    # CHANGE THESE
    auth_email="john.appleseed@example.org" # The email used to login 'https://dash.cloudflare.com'
    auth_key="f1nd7h47fuck1n6k3y1ncl0udfl4r3c0n50l3" # Top right corner, "My profile" > "Global API Key"
    zone_identifier="f1nd7h3fuck1n6z0n31d3n71f13r4l50" # Can be found in the "Overview" tab of your domain
    record_name="ipv4.example.org" # Which record you want to be synced

    # DO NOT CHANGE LINES BELOW
    ip4=$(curl -s https://ipv4.icanhazip.com/)
    ip6=$(curl -s https://ipv46.icanhazip.com/)

    # SCRIPT START
    echo "[Cloudflare DDNS] Check Initiated"

    # Seek for the record
    record4=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=A" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json")
    record6=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records?name=$record_name&type=AAAA" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json")

    # Can't do anything without both record
    if [[ $record4 == *"\"count\":0"* || $record6 == *"\"count\":0"* ]]; then
    >&2 echo -e "[Cloudflare DDNS] Dual stack records do not exist, perhaps create them first?"
    exit 1
    fi

    # Set existing IP address from the fetched record
    old_ip4=$(echo "$record4" | grep -Po '(?<="content":")[^"]*' | head -1)
    old_ip6=$(echo "$record6" | grep -Po '(?<="content":")[^"]*' | head -1)

    # Compare either one is the same
    # NOTE: The script will update even one IP remains the same.
    if [ $ip4 == $old_ip4 && $ip6 == $old_ip6 ]; then
    echo "[Cloudflare DDNS] IPs have not changed."
    exit 0
    fi

    # Set the record identifier from result
    record4_identifier=$(echo "$record4" | grep -Po '(?<="id":")[^"]*' | head -1)
    record6_identifier=$(echo "$record6" | grep -Po '(?<="id":")[^"]*' | head -1)

    # The execution of update
    update4=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier4" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"A\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip4\"}")
    update6=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier6" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"id\":\"$zone_identifier\",\"type\":\"AAAA\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip6\"}")

    # The moment of truth
    if [[ $update4 == *"\"success\":false"* || $update6 == *"\"success\":false"* ]]; then
    >&2 echo -e "[Cloudflare DDNS] Update failed. DUMPING RESULTS:\n$update4\n$update6"
    exit 1
    else
    echo "[Cloudflare DDNS] IPv4 address '$ip4' and IPv6 address '$ip6' has been synced to Cloudflare."
    fi
    10 changes: 5 additions & 5 deletions cfupdater-v4
    Original file line number Diff line number Diff line change
    @@ -11,14 +11,14 @@ record_name="ipv4.example.org" # Which record you want to be
    ip=$(curl -s https://ipv4.icanhazip.com/)

    # SCRIPT START
    echo "[Cloudflare DDNS] Check Initiated" | systemd-cat
    echo "[Cloudflare DDNS] Check Initiated"

    # Seek for the record
    record=$(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")

    # Can't do anything without the record
    if [[ $record == *"\"count\":0"* ]]; then
    echo -e "[Cloudflare DDNS] Record does not exist, perhaps create one first?" | systemd-cat -p crit
    >&2 echo -e "[Cloudflare DDNS] Record does not exist, perhaps create one first?"
    exit 1
    fi

    @@ -27,7 +27,7 @@ old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)

    # Compare if they're the same
    if [ $ip == $old_ip ]; then
    echo "[Cloudflare DDNS] IP has not changed." | systemd-cat -p notice
    echo "[Cloudflare DDNS] IP has not changed."
    exit 0
    fi

    @@ -40,8 +40,8 @@ update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identi
    # The moment of truth
    case "$update" in
    *"\"success\":false"*)
    echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update" | systemd-cat -p err
    >&2 echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update"
    exit 1;;
    *)
    echo "[Cloudflare DDNS] IP synced to: $ip" | systemd-cat -p notice;;
    echo "[Cloudflare DDNS] IPv4 context '$ip4' has been synced to Cloudflare.";;
    esac
    8 changes: 4 additions & 4 deletions cfupdater-v6
    Original file line number Diff line number Diff line change
    @@ -11,14 +11,14 @@ record_name="ipv6.example.org" # Which record you want to be
    ip=$(curl -s https://ipv6.icanhazip.com/)

    # SCRIPT START
    echo "[Cloudflare DDNS] Check Initiated" | systemd-cat
    echo "[Cloudflare DDNS] Check Initiated"

    # Seek for the record
    record=$(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")

    # Can't do anything without the record
    if [[ $record == *"\"count\":0"* ]]; then
    echo -e "[Cloudflare DDNS] Record does not exist, perhaps create one first?" | systemd-cat -p crit
    >&2 echo -e "[Cloudflare DDNS] Record does not exist, perhaps create one first?"
    exit 1
    fi

    @@ -40,8 +40,8 @@ update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identi
    # The moment of truth
    case "$update" in
    *"\"success\":false"*)
    echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update" | systemd-cat -p err
    >&2 echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update"
    exit 1;;
    *)
    echo "[Cloudflare DDNS] IP synced to: $ip" | systemd-cat -p notice;;
    echo "[Cloudflare DDNS] IPv6 address '$ip6' has been synced to Cloudflare.";;
    esac
  7. @lifehome lifehome revised this gist Jul 20, 2018. 3 changed files with 51 additions and 2 deletions.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,9 @@
    This is a bash script to act as a Cloudflare DDNS client, useful replacement for ddclient.

    # How to use?
    1) Put the `cfupdater` file to `/usr/local/bin`
    1) Put the `cfupdater` files to `/usr/local/bin`
    - If you are using IPv4 for A record, append `-v4` to `cfupdater` in the following systemd service unit.
    - If you are using IPv6 for AAAA record, append `-v6` to `cfupdater` in the following systemd service unit.
    2) `chmod +x /usr/local/bin/cfupdater`
    3) Create a systemd service unit at `/etc/systemd/system/`, the `cfupdate.service` is shown as an example.
    4) Create a systemd timer unit at **the same location of the service unit**, the `cfupdate.timer` is shown as an example.
    2 changes: 1 addition & 1 deletion cfupdater → cfupdater-v4
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    auth_email="john.appleseed@example.org" # The email used to login 'https://dash.cloudflare.com'
    auth_key="f1nd7h47fuck1n6k3y1ncl0udfl4r3c0n50l3" # Top right corner, "My profile" > "Global API Key"
    zone_identifier="f1nd7h3fuck1n6z0n31d3n71f13r4l50" # Can be found in the "Overview" tab of your domain
    record_name="example.org" # Which record you want to be synced
    record_name="ipv4.example.org" # Which record you want to be synced

    # DO NOT CHANGE LINES BELOW
    ip=$(curl -s https://ipv4.icanhazip.com/)
    47 changes: 47 additions & 0 deletions cfupdater-v6
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/bash

    # Forked by benkulbertis/cloudflare-update-record.sh
    # CHANGE THESE
    auth_email="john.appleseed@example.org" # The email used to login 'https://dash.cloudflare.com'
    auth_key="f1nd7h47fuck1n6k3y1ncl0udfl4r3c0n50l3" # Top right corner, "My profile" > "Global API Key"
    zone_identifier="f1nd7h3fuck1n6z0n31d3n71f13r4l50" # Can be found in the "Overview" tab of your domain
    record_name="ipv6.example.org" # Which record you want to be synced

    # DO NOT CHANGE LINES BELOW
    ip=$(curl -s https://ipv6.icanhazip.com/)

    # SCRIPT START
    echo "[Cloudflare DDNS] Check Initiated" | systemd-cat

    # Seek for the record
    record=$(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")

    # Can't do anything without the record
    if [[ $record == *"\"count\":0"* ]]; then
    echo -e "[Cloudflare DDNS] Record does not exist, perhaps create one first?" | systemd-cat -p crit
    exit 1
    fi

    # Set existing IP address from the fetched record
    old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)

    # Compare if they're the same
    if [ $ip == $old_ip ]; then
    echo "[Cloudflare DDNS] IP has not changed." | systemd-cat -p notice
    exit 0
    fi

    # Set the record identifier from result
    record_identifier=$(echo "$record" | grep -Po '(?<="id":")[^"]*' | head -1)

    # The execution of update
    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\":\"AAAA\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip\"}")

    # The moment of truth
    case "$update" in
    *"\"success\":false"*)
    echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update" | systemd-cat -p err
    exit 1;;
    *)
    echo "[Cloudflare DDNS] IP synced to: $ip" | systemd-cat -p notice;;
    esac
  8. @lifehome lifehome revised this gist Jul 15, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cfupdater
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@ record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identi

    # Can't do anything without the record
    if [[ $record == *"\"count\":0"* ]]; then
    echo -e "Record does not exist, perhaps create one first?" | systemd-cat -p crit
    echo -e "[Cloudflare DDNS] Record does not exist, perhaps create one first?" | systemd-cat -p crit
    exit 1
    fi

    @@ -27,7 +27,7 @@ old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)

    # Compare if they're the same
    if [ $ip == $old_ip ]; then
    echo "IP has not changed." | systemd-cat -p notice
    echo "[Cloudflare DDNS] IP has not changed." | systemd-cat -p notice
    exit 0
    fi

  9. @lifehome lifehome revised this gist Jul 14, 2018. 4 changed files with 37 additions and 5 deletions.
    17 changes: 17 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # Cloudflare DDNS bash client with systemd
    This is a bash script to act as a Cloudflare DDNS client, useful replacement for ddclient.

    # How to use?
    1) Put the `cfupdater` file to `/usr/local/bin`
    2) `chmod +x /usr/local/bin/cfupdater`
    3) Create a systemd service unit at `/etc/systemd/system/`, the `cfupdate.service` is shown as an example.
    4) Create a systemd timer unit at **the same location of the service unit**, the `cfupdate.timer` is shown as an example.

    # Note
    The default `cfupdate.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.
    >
    > -- https://support.cloudflare.com/hc/en-us/articles/200171456
    10 changes: 10 additions & 0 deletions cfupdate.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    [Unit]
    Description=Cloudflare DDNS service
    After=network.target

    [Service]
    Type=oneshot
    ExecStart=/usr/local/bin/cfupdater

    [Install]
    WantedBy=multi-user.target
    5 changes: 5 additions & 0 deletions cfupdate.timer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    [Unit]
    Description=Run cfupdate.service every minute

    [Timer]
    OnCalendar=*:0/1
    10 changes: 5 additions & 5 deletions cfupdater
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ zone_identifier="f1nd7h3fuck1n6z0n31d3n71f13r4l50" # Can be found in the "Overvi
    record_name="example.org" # Which record you want to be synced

    # DO NOT CHANGE LINES BELOW
    ip=$(curl -s https://v4.ifconfig.co/ip)
    ip=$(curl -s https://ipv4.icanhazip.com/)

    # SCRIPT START
    echo "[Cloudflare DDNS] Check Initiated" | systemd-cat
    @@ -18,7 +18,7 @@ record=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_identi

    # Can't do anything without the record
    if [[ $record == *"\"count\":0"* ]]; then
    echo -e "Record does not exist, perhaps create one first?" | systemd-cat -p emerg
    echo -e "Record does not exist, perhaps create one first?" | systemd-cat -p crit
    exit 1
    fi

    @@ -27,7 +27,7 @@ old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)

    # Compare if they're the same
    if [ $ip == $old_ip ]; then
    echo "IP has not changed."
    echo "IP has not changed." | systemd-cat -p notice
    exit 0
    fi

    @@ -40,8 +40,8 @@ update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identi
    # The moment of truth
    case "$update" in
    *"\"success\":false"*)
    echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update" | systemd-cat -p emerg
    echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update" | systemd-cat -p err
    exit 1;;
    *)
    echo "[Cloudflare DDNS] IP synced to: $ip" | systemd-cat;;
    echo "[Cloudflare DDNS] IP synced to: $ip" | systemd-cat -p notice;;
    esac
  10. @lifehome lifehome renamed this gist Jul 6, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  11. @lifehome lifehome revised this gist Jul 6, 2018. 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
    @@ -16,6 +16,7 @@ echo "[Cloudflare DDNS] Check Initiated" | systemd-cat
    # Seek for the record
    record=$(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")

    # Can't do anything without the record
    if [[ $record == *"\"count\":0"* ]]; then
    echo -e "Record does not exist, perhaps create one first?" | systemd-cat -p emerg
    exit 1
  12. @lifehome lifehome revised this gist Jul 6, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@ record_name="example.org" # Which record you want to be

    # DO NOT CHANGE LINES BELOW
    ip=$(curl -s https://v4.ifconfig.co/ip)
    log_file="/opt/cfupdate/cloudflare.log"

    # SCRIPT START
    echo "[Cloudflare DDNS] Check Initiated" | systemd-cat
  13. @lifehome lifehome revised this gist Jul 6, 2018. 1 changed file with 38 additions and 46 deletions.
    84 changes: 38 additions & 46 deletions cloudflare-update-record.sh
    Original file line number Diff line number Diff line change
    @@ -1,55 +1,47 @@
    #!/bin/bash

    # Forked by benkulbertis/cloudflare-update-record.sh
    # CHANGE THESE
    auth_email="user@example.com"
    auth_key="c2547eb745079dac9320b638f5e225cf483cc5cfdda41" # found in cloudflare account settings
    zone_name="example.com"
    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
    log() {
    if [ "$1" ]; then
    echo -e "[$(date)] - $1" >> $log_file
    fi
    }
    auth_email="john.appleseed@example.org" # The email used to login 'https://dash.cloudflare.com'
    auth_key="f1nd7h47fuck1n6k3y1ncl0udfl4r3c0n50l3" # Top right corner, "My profile" > "Global API Key"
    zone_identifier="f1nd7h3fuck1n6z0n31d3n71f13r4l50" # Can be found in the "Overview" tab of your domain
    record_name="example.org" # Which record you want to be synced

    # DO NOT CHANGE LINES BELOW
    ip=$(curl -s https://v4.ifconfig.co/ip)
    log_file="/opt/cfupdate/cloudflare.log"

    # 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
    echo "[Cloudflare DDNS] Check Initiated" | systemd-cat

    # Seek for the record
    record=$(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")

    if [[ $record == *"\"count\":0"* ]]; then
    echo -e "Record does not exist, perhaps create one first?" | systemd-cat -p emerg
    exit 1
    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" | 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
    # Set existing IP address from the fetched record
    old_ip=$(echo "$record" | grep -Po '(?<="content":")[^"]*' | head -1)

    # Compare if they're the same
    if [ $ip == $old_ip ]; then
    echo "IP has not changed."
    exit 0
    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
    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
    # Set the record identifier from result
    record_identifier=$(echo "$record" | grep -Po '(?<="id":")[^"]*' | head -1)

    # The execution of update
    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\",\"proxied\":false,\"name\":\"$record_name\",\"content\":\"$ip\"}")

    # The moment of truth
    case "$update" in
    *"\"success\":false"*)
    echo -e "[Cloudflare DDNS] Update failed for $record_identifier. DUMPING RESULTS:\n$update" | systemd-cat -p emerg
    exit 1;;
    *)
    echo "[Cloudflare DDNS] IP synced to: $ip" | systemd-cat;;
    esac
  14. @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\"}")

  15. @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
  16. @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
  17. @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
  18. @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
  19. @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
  20. @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
  21. @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="user@example.com"
    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: user@example.com" -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
  22. @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: user@example.com" -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
  23. @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"
  24. @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: user@example.com" -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