gstuartj / cf-ddns.sh

A simple client for automatically updating CloudFlare DNS with your current IP address
MIT License
81 stars 35 forks source link

Doesn't add record #4

Open bighippo999 opened 7 years ago

bighippo999 commented 7 years ago

Hi, great script thanks for writing it. I need to automatically add dns records for new servers, but I was getting update failed error when running. Looking through the code and Cloudflares API guide it looks like the script can only update an existing record (it uses PUT), I added an if check to see if it's adding/updating then use either POST or PUT depending on the action.

I genuinely dont understand GIT to submit my changes back :) so I'll just put it below:

# Perform record update
api_dns_update=`${curl_command} -s -X PUT "${cf_api_url}/zones/${zone_id}/dns_records/${record_id}" -H "X-Auth-Email: ${cf_email}" -H "X-Auth-Key: ${cf_api_key}" -H "Content-Type: application/json" --data "{\"id\":\"${zone_id}\",\"type\":\"A\",\"name\":\"${record_name}\",\"content\":\"${WAN_addr}\"}"`

Changed to:

# Perform record update
 if [ -z $record_id ]; then
     api_dns_update=`${curl_command} -s -X POST "${cf_api_url}/zones/${zone_id}/dns_records" -H "X-Auth-Email: ${cf_email}" -H "X-Auth-Key: ${cf_api_key}" -H "Content-Type: application/json" --data "{\"id\":\"${zone_id}\",\"type\":\"A\",\"name\":\"${record_name}\",\"content\":\"${WAN_addr}\"}"`
 else
     api_dns_update=`${curl_command} -s -X PUT "${cf_api_url}/zones/${zone_id}/dns_records/${record_id}" -H "X-Auth-Email: ${cf_email}" -H "X-Auth-Key: ${cf_api_key}" -H "Content-Type: application/json" --data "{\"id\":\"${zone_id}\",\"type\":\"A\",\"name\":\"${record_name}\",\"content\":\"${WAN_addr}\"}"`
 fi

Thanks again for writing it, I hope this helps someone.

shimco commented 6 years ago

This script is great, but on Mac OSX updating a record was failing with the error below, even after I manually created the record on the cloudflare site: Record update failed. {"success":false,"errors":[{"code":7001,"message":"Method PUT not available for that URI."}],"messages":[],"result":null}

Updating this line has fixed the error and it's now working! Thanks @bighippo999 I've submitted a pull request to add this.