Forked from benkulbertis/cloudflare-update-record.sh
Last active
December 4, 2022 15:55
-
-
Save 4ft35t/510897486bc6986d19cac45b3b9ca1d0 to your computer and use it in GitHub Desktop.
Revisions
-
4ft35t revised this gist
Dec 4, 2022 . 1 changed file with 51 additions and 51 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,55 +1,55 @@ #!/bin/sh ## 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 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}' -
4ft35t revised this gist
Apr 14, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 "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) -
4ft35t revised this gist
Sep 20, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #!/bin/sh # CHANGE THESE auth_email="[email protected]" -
4ft35t revised this gist
Sep 20, 2018 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" | 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" -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 10 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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\"}") -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ log_file="cloudflare.log" # LOGGER log() { if [ "$1" ]; then 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:\n$update" echo "API UPDATE FAILED. DUMPING RESULTS:" echo "$update" exit 1 -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 17 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 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 log "API UPDATE FAILED. DUMPING RESULTS:" log "$update" echo "API UPDATE FAILED. DUMPING RESULTS:" echo "$update" exit 1 else echo "$ip" > $ip_file log "IP changed to: $ip" echo "IP changed to: $ip" fi -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 8 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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":")[^"]*') 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 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" fi -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 13 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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" echo "[$(date)] - Check Initiated" >> $log_file if [ -f $ip_file ]; then @@ -20,12 +22,16 @@ 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 if [ $? -eq 0 ]; then 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 -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 "IP changed to: $ip" else echo "[$(date)] - API UPDATE FAILED WTF SON" >> $log_file echo "API UPDATE FAILED WTF SON." fi -
benkulbertis revised this gist
May 4, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/bin/bash ip=$(curl -s http://ipv4.icanhazip.com) ip_file="ip.txt" log_file="cloudflare.log" zone_identifier="ZONE IDENTIFIER" -
benkulbertis created this gist
May 4, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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