write a bash script to add a dns cname record on digital ocean using their api and curl
- User
write a bash script to add a dns cname record on digital ocean using their api and curl
- Assistant
Sure! Here's an example of a bash script that uses the DigitalOcean API and curl to add a DNS CNAME record:
#!/bin/bash # Replace the following variables with your own values API_TOKEN="YOUR_DIGITALOCEAN_API_TOKEN" DOMAIN="yourdomain.com" RECORD_NAME="subdomain" RECORD_VALUE="example.com" # Create the JSON payload DATA="{\"type\":\"CNAME\",\"name\":\"$RECORD_NAME\",\"data\":\"$RECORD_VALUE\",\"ttl\":1800}" # Send the API request to create the DNS record response=$(curl -X POST "https://api.digitalocean.com/v2/domains/$DOMAIN/records" \ -H "Authorization: Bearer $API_TOKEN" \ -H "Content-Type: application/json" \ -d "$DATA") # Check the response status if [ "$(echo "$response" | jq -r '.id')" != null ]; then echo "CNAME record created successfully." else echo "Failed to create CNAME record." echo "Response: $response" fiMake sure to replace
YOUR_DIGITALOCEAN_API_TOKENwith your own DigitalOcean API token. Replaceyourdomain.comwith the domain you want to add the CNAME record to. Replacesubdomainwith the name of the subdomain you want to create, andexample.comwith the value (target) for the CNAME record.Save the script to a file (e.g.,
add_dns_cname.sh), and make it executable with the following command:chmod +x add_dns_cname.shTo execute the script, run:
./add_dns_cname.shThe script will send an API request to create a DNS CNAME record using the provided values. It will check the response from the API and display a success message or an error message accordingly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.