Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

write a bash script to add a dns cname record on digital ocean using their api and curl

  1. User

    write a bash script to add a dns cname record on digital ocean using their api and curl

  2. 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"
    fi
    

    Make sure to replace YOUR_DIGITALOCEAN_API_TOKEN with your own DigitalOcean API token. Replace yourdomain.com with the domain you want to add the CNAME record to. Replace subdomain with the name of the subdomain you want to create, and example.com with 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.sh
    

    To execute the script, run:

    ./add_dns_cname.sh
    

    The 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.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.