Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % cat update_cloudflare.sh
- #!/bin/bash
- # Last modified: 4 May, 2020
- # Written by: Me
- # This script makes a call to whatismyipaddress and then updates Cloudflare
- # with the external IP addresses.
- # Cloudflare API is pretty extensive. Documentation is here:
- # https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record
- # WhatIsMyIPAddress documentation is here:
- # https://whatismyipaddress.com/api
- # Note that whatismyipaddress does not want requests more frequent than once
- # every 5 minutes.
- #Notes
- # All requests must include both X-AUTH-KEY and X-AUTH-EMAIL headers.
- # Requests that use X-AUTH-USER-SERVICE-KEY can use that instead.
- #Global
- api_key='redacted'
- service_key='redacted'
- base_url='https://api.cloudflare.com/client/v4'
- email='[email protected]'
- my_zoneid='redacted'
- my_accountid='redacted'
- # These tokens are only viewable once! Don't delete them!
- # ... But I'm not sure what they're used for.
- token1='redacted'
- token2='redacted'
- # These functions are working cURL calls that demonstrate usage.
- test_tokens () {
- echo "Testing first token, ${token1}"
- curl -X GET "${base_url}/user/tokens/verify" \
- -H "Authorization: Bearer ${token1}" \
- -H "Content-Type:application/json"
- echo ""
- echo "Testing second token, ${token2}"
- curl -X GET "${base_url}/user/tokens/verify" \
- -H "Authorization: Bearer ${token2}" \
- -H "Content-Type:application/json"
- }
- get_zone_id () {
- curl -X GET "${base_url}/zones" \
- -H "X-Auth-Email: ${email}" \
- -H "X-Auth-Key: ${api_key}" \
- -H "Content-Type: application/json"
- }
- get_zone_details () {
- curl -X GET "${base_url}/zones/${my_zoneid}" \
- -H "X-Auth-Email: ${email}" \
- -H "X-Auth-Key: ${api_key}" \
- -H "Content-Type: application/json"
- }
- # This is a useful function since record id's are only available via the API.
- get_zone_records () {
- curl -X GET "${base_url}/zones/${my_zoneid}/dns_records" \
- -H "X-Auth-Email: ${email}" \
- -H "X-Auth-Key: ${api_key}" \
- -H "Content-Type: application/json"
- }
- update_records () {
- # See the output of get_zone_records to find the ids of the individual records.
- local main='redacted'
- local secondary='redacted'
- curl -s -S -X PUT "${base_url}/zones/${my_zoneid}/dns_records/${main}" \
- -H "X-Auth-Email: ${email}" \
- -H "X-Auth-Key: ${api_key}" \
- -H "Content-Type: application/json" \
- --data "{\"type\":\"A\",\"name\":\"mydomain.com\",\"content\":\"${external_ip}\",\"ttl\":1}" > /dev/null
- curl -s -S -X PUT "${base_url}/zones/${my_zoneid}/dns_records/${secondary}" \
- -H "X-Auth-Email: ${email}" \
- -H "X-Auth-Key: ${api_key}" \
- -H "Content-Type: application/json" \
- --data "{\"type\":\"A\",\"name\":\"subdomain.mydomain.com\",\"content\":\"${external_ip}\",\"ttl\":1}" > /dev/null
- }
- external_ip=`curl -s -S -H "User-Agent: ${my_email}" https://ipv4bot.whatismyipaddress.com`
- update_records
- exit
Add Comment
Please, Sign In to add comment