wmarler

Update Cloudflare with bash

May 4th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.97 KB | None | 0 0
  1. % cat update_cloudflare.sh
  2. #!/bin/bash
  3. # Last modified: 4 May, 2020
  4. # Written by: Me
  5. # This script makes a call to whatismyipaddress and then updates Cloudflare
  6. # with the external IP addresses.
  7.  
  8. # Cloudflare API is pretty extensive. Documentation is here:
  9. # https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
  10.  
  11. # WhatIsMyIPAddress documentation is here:
  12. # https://whatismyipaddress.com/api
  13. # Note that whatismyipaddress does not want requests more frequent than once
  14. # every 5 minutes.
  15.  
  16. #Notes
  17. # All requests must include both X-AUTH-KEY and X-AUTH-EMAIL headers.
  18. # Requests that use X-AUTH-USER-SERVICE-KEY can use that instead.
  19.  
  20. #Global
  21. api_key='redacted'
  22. service_key='redacted'
  23. base_url='https://api.cloudflare.com/client/v4'
  24. my_zoneid='redacted'
  25. my_accountid='redacted'
  26.  
  27. # These tokens are only viewable once! Don't delete them!
  28. # ... But I'm not sure what they're used for.
  29. token1='redacted'
  30. token2='redacted'
  31.  
  32. # These functions are working cURL calls that demonstrate usage.
  33. test_tokens () {
  34.   echo "Testing first token, ${token1}"
  35.   curl -X GET "${base_url}/user/tokens/verify" \
  36.        -H "Authorization: Bearer ${token1}" \
  37.        -H "Content-Type:application/json"
  38.   echo ""
  39.   echo "Testing second token, ${token2}"
  40.   curl -X GET "${base_url}/user/tokens/verify" \
  41.        -H "Authorization: Bearer ${token2}" \
  42.        -H "Content-Type:application/json"
  43. }
  44.  
  45. get_zone_id () {
  46.   curl -X GET "${base_url}/zones" \
  47.        -H "X-Auth-Email: ${email}" \
  48.        -H "X-Auth-Key: ${api_key}" \
  49.        -H "Content-Type: application/json"
  50. }
  51.  
  52. get_zone_details () {
  53.   curl -X GET "${base_url}/zones/${my_zoneid}" \
  54.        -H "X-Auth-Email: ${email}" \
  55.        -H "X-Auth-Key: ${api_key}" \
  56.        -H "Content-Type: application/json"
  57. }
  58.  
  59. # This is a useful function since record id's are only available via the API.
  60. get_zone_records () {
  61.   curl -X GET "${base_url}/zones/${my_zoneid}/dns_records" \
  62.        -H "X-Auth-Email: ${email}" \
  63.        -H "X-Auth-Key: ${api_key}" \
  64.        -H "Content-Type: application/json"
  65. }
  66.  
  67. update_records () {
  68.   # See the output of get_zone_records to find the ids of the individual records.
  69.   local main='redacted'
  70.   local secondary='redacted'
  71.   curl -s -S -X PUT "${base_url}/zones/${my_zoneid}/dns_records/${main}" \
  72.        -H "X-Auth-Email: ${email}" \
  73.        -H "X-Auth-Key: ${api_key}" \
  74.        -H "Content-Type: application/json" \
  75.        --data "{\"type\":\"A\",\"name\":\"mydomain.com\",\"content\":\"${external_ip}\",\"ttl\":1}" > /dev/null
  76.   curl -s -S -X PUT "${base_url}/zones/${my_zoneid}/dns_records/${secondary}" \
  77.        -H "X-Auth-Email: ${email}" \
  78.        -H "X-Auth-Key: ${api_key}" \
  79.        -H "Content-Type: application/json" \
  80.        --data "{\"type\":\"A\",\"name\":\"subdomain.mydomain.com\",\"content\":\"${external_ip}\",\"ttl\":1}" > /dev/null
  81. }
  82.  
  83. external_ip=`curl -s -S -H "User-Agent: ${my_email}" https://ipv4bot.whatismyipaddress.com`
  84. update_records
  85.  
  86. exit
Add Comment
Please, Sign In to add comment