This Bash script is designed to automatically update the "A" record for a specified domain name in Cloudflare. "A" records are DNS records that point your domain name to an IP address. This script uses Cloudflare's HTTP API to perform these updates.
Here's what each part of the script does:
- The
API_TOKENis passed to the script as a command line argument. This token is a unique key that allows the script to interact with the Cloudflare API. - The current date and time are fetched and saved to the
current_timevariable. This is used later in the script to mark when updates were performed. - The current public IP of the machine running the script is retrieved using the Amazon's Check IP service and stored in the
public_ipvariable. - Various variables are defined:
ZONE_IDis the unique identifier for your Cloudflare zone (which usually contains one or more domain names),RECORD_NAMEis the domain name for which the DNS record is to be updated. - The
H1,H2, andH3variables are used to store the headers for the HTTP requests made to Cloudflare's API. - The script makes a GET request to the Cloudflare API to retrieve all DNS records for the
ZONE_ID. - From the returned DNS records, the
RECORD_IDfor the requiredRECORD_NAMEis extracted. - If no
RECORD_IDis found, the script prints an error message and stops execution. - The script checks if the current public IP of the machine is different from that in the DNS record.
- If the IP address is different, the script generates a JSON payload with the new IP address and updates the DNS record through the Cloudflare API.
To use this script, you need to replace the following placeholders:
- Replace
xxxxxxxxxxxxxxxxxxxxxxwith your Cloudflare Zone ID. - Replace
record.domain.comwith the domain name you wish to update. - Replace
youremail@address.comwith your email.
To use the script, copy this script to your local machine:
- Create a new file using any text editor and paste the script into that file.
- Save the file with an appropriate name, such as
homeservice.greatdomain.com.sh.
To change permissions of the file so it can run as a script:
chmod +x homeservice.greatdomain.com.shThe above command makes the script executable. You can execute the script using the command below, ensuring to replace <your-api-token> with your actual Cloudflare API token:
./homeservice.greatdomain.com.sh <your-api-token>