If you are using Cloudflare as your CDN or proxy to a server with no reserved IP address from a cloud provider, you could use this command line utility script to update your records as desired. Most often, you would want to update your Cloudflare records when the server is switched on, with the newly attributed IP address, without having to connect to Cloudflare(manually). This tool offers an update
command that allows doing just that.
Say you have the following records on Cloudflare:
The test
server is frequently switched on and off and its IP address changes every time it is started. Running this utility by passing it a --record-id
argument of the target record and a --payload
containing a JSON string of the properties and values of the record to update will apply the change to your record.
- Make sure you have
wget
or similar tool installed on your machine - Have Python 3 installed
- Add your Cloudflare configurations to your environment variables as follows:
export CLOUDFLARE_AUTH_EMAIL="Cloudflare account email"
export CLOUDFLARE_AUTH_KEY="Cloudflare API Key"
export CLOUDFLARE_ZONE_ID="Your zone ID"
wget -O - https://raw.githubusercontent.com/Olfredos6/cloudflary.py/cli/cloudflary.py | python3 - [command] [arguments]
- list: List all records on the specified zone(See ZONE_ID).
list
- update: Patch/Update a DNS record. This can be any value on the DNS record.
update --record-id RECORD_ID --payload '{"key": "value"}'
- get: Get details of a DNS record.
get --record-id RECORD_ID
- Simple example:
wget -O - https://raw.githubusercontent.com/Olfredos6/cloudflary.py/cli/cloudflary.py |python3 - update --record-id 8ec292220081262ca459013e40f80df5 --payload '{"content": "142.251.47.78"}'
142.251.47.78 is Google 😅
- One more realistic example: Automatically grab host's public IP from an Amazon EC2 instance and update a Cloudflare record
wget -O - https://raw.githubusercontent.com/Olfredos6/cloudflary.py/cli/cloudflary.py |python3 - update --record-id 8ec292220081262ca459013e40f80df5 --payload "{\"content\": \"$(curl -s ifconfig.me)\"}"