Spara upp till 81% vid första året på webbhotell!

Användarapi – exempel för DNS hantering

Ett exempelskript för användarapi som visar hur du kan hantera DNS poster via API.

Vi erbjuder ett öppet API för att hantera DNS-poster. Detta är praktiskt om du exempelvis vill bygga egen dynamisk DNS eller automatisera uppdateringar vid SSL-förnyelse via ACME/Let’s Encrypt.

För att hantera DNS-poster använder du DNS-sektionen i API:t. Nedan finns ett bash-skript som demonstrerar hur detta kan gå till. Skriptet kan lista, ta bort, lägga till och uppdatera DNS-poster för ett domännamn via enkla kommandon.

Exempelkommandon:

Lista DNS-poster för ett domännamn:

./hostup-dns api-testing.test

Lägg till en A-post:

  • api-testing.test – postens namn
  • A – posttyp
  • 192.168.1.2 – värde
  • 3600 – TTL
  • 0 – prioritet
./hostup-dns add api-testing.test api-testing.test A 192.168.1.2 3600 0

Uppdatera en befintlig A-post:
(obs: 14306113 är postens ID, du får den via listkommandot)

./hostup-dns update api-testing.test 14306113 api-testing.test A 192.168.1.2 3600 0

Ta bort en A-post:

./hostup-dns delete api-testing.test 14306113

Installera jq:

För att kunna använda bash skriptet nedan behöver du först installera jq.

macOS:

brew install jq

ubuntu/debian:

apt install jq

Byt ut USERNAME och PASSWORD i skriptet nedan till dina inloggningsuppgifter. Alternativt kan du skapa en separat användare med begränsade rättigheter här:
https://min.hostup.se/profiles/

Bash-skript:

Kopiera, spara som hostup-dns och gör körbar med chmod +x hostup-dns:

#!/bin/bash

# Authentication details (replace these with your actual credentials)
USERNAME="REPLACEME"
PASSWORD="REPLACEME"

# URL for DNS API
DNS_API_URL="https://min.hostup.se/api/dns"

# Function to display help
display_help() {
    echo "Usage: ./hostup-dns [command] [arguments]"
    echo
    echo "Commands:"
    echo "  --help                  Display this help and exit"
    echo "  no arguments            List all domains with service ID and domain ID"
    echo "  [domain]                List all DNS records for the specified domain"
    echo "  add [domain_name] [record_name] [record_type] [record_content] [record_ttl] [record_priority]"
    echo "                          Add a new DNS record"
    echo "  update [domain_name] [record_id] [record_name] [record_type] [record_content] [record_ttl] [record_priority]"
    echo "                          Update an existing DNS record"
    echo "  delete [domain_name] [record_id]"
    echo "                          Delete a DNS record"
    echo
    echo "Examples:"
    echo "  ./hostup-dns"
    echo "  ./hostup-dns api-testing.test"
    echo "  ./hostup-dns add api-testing.test api-testing.test A 192.168.1.2 3600 0"
    echo "  ./hostup-dns update api-testing.test 14306113 api-testing.test A 192.168.1.2 3600 0"
    echo "  ./hostup-dns delete api-testing.test 14306113"
}

# Function to list domains along with service id and domain id
list_domains() {
    response=$(curl -s -u "$USERNAME:$PASSWORD" "$DNS_API_URL")

    echo "$response" | jq -r '.zones[] | "Domain: \(.name), Service ID: \(.service_id), Domain ID: \(.domain_id)"'
}

# Function to list DNS records for a specific domain
list_dns_records() {
    domain_to_search="$1"
    response=$(curl -s -u "$USERNAME:$PASSWORD" "$DNS_API_URL")
    
    # Find the domain id for the provided domain name
    domain_id=$(echo "$response" | jq -r ".zones[] | select(.name==\"$domain_to_search\") | .domain_id")
    service_id=$(echo "$response" | jq -r ".zones[] | select(.name==\"$domain_to_search\") | .service_id")

    if [ -z "$domain_id" ]; then
        echo "Domain not found or you do not have permission to view its records."
        exit 1
    fi

    # Fetch DNS records for the specific domain
    records_response=$(curl -s -u "$USERNAME:$PASSWORD" "https://min.hostup.se/api/service/$service_id/dns/$domain_id")
    
    echo "$records_response" | jq -r '.records[] | "Record ID: \(.id), Type: \(.type), Name: \(.name), Content: \(.content), TTL: \(.ttl)"'
}

# Function to add a new DNS record
add_dns_record() {
    domain_name="$1"
    record_name="$2"
    record_type="$3"
    record_content="$4"
    record_ttl="$5"
    record_priority="$6"

    response=$(curl -s -u "$USERNAME:$PASSWORD" "$DNS_API_URL")

    domain_id=$(echo "$response" | jq -r ".zones[] | select(.name==\"$domain_name\") | .domain_id")
    service_id=$(echo "$response" | jq -r ".zones[] | select(.name==\"$domain_name\") | .service_id")

    if [ -z "$domain_id" ]; then
        echo "Domain not found or you do not have permission to add records."
        exit 1
    fi

    POST_DATA="$(jq -n \
        --arg name "$record_name" \
        --arg type "$record_type" \
        --argjson ttl "$record_ttl" \
        --argjson priority "$record_priority" \
        --arg content "$record_content" \
        '{name: $name, type: $type, ttl: $ttl, priority: $priority, content: $content}')"

    curl -X POST "https://min.hostup.se/api/service/$service_id/dns/$domain_id/records" \
        -u "$USERNAME:$PASSWORD" \
        -H "Content-Type: application/json" \
        -d "$POST_DATA"
}

# Function to update an existing DNS record
update_dns_record() {
    domain_name="$1"
    record_id="$2"
    record_name="$3"
    record_type="$4"
    record_content="$5"
    record_ttl="$6"
    record_priority="$7"

    response=$(curl -s -u "$USERNAME:$PASSWORD" "$DNS_API_URL")

    domain_id=$(echo "$response" | jq -r ".zones[] | select(.name==\"$domain_name\") | .domain_id")
    service_id=$(echo "$response" | jq -r ".zones[] | select(.name==\"$domain_name\") | .service_id")

    if [ -z "$domain_id" ]; then
        echo "Domain not found or you do not have permission to update records."
        exit 1
    fi

    POST_DATA="$(jq -n \
        --arg name "$record_name" \
        --arg type "$record_type" \
        --argjson ttl "$record_ttl" \
        --argjson priority "$record_priority" \
        --arg content "$record_content" \
        '{name: $name, type: $type, ttl: $ttl, priority: $priority, content: $content}')"

    curl -X PUT "https://min.hostup.se/api/service/$service_id/dns/$domain_id/records/$record_id" \
        -u "$USERNAME:$PASSWORD" \
        -H "Content-Type: application/json" \
        -d "$POST_DATA"
}

# Function to delete a DNS record
delete_dns_record() {
    domain_name="$1"
    record_id="$2"

    response=$(curl -s -u "$USERNAME:$PASSWORD" "$DNS_API_URL")

    domain_id=$(echo "$response" | jq -r ".zones[] | select(.name==\"$domain_name\") | .domain_id")
    service_id=$(echo "$response" | jq -r ".zones[] | select(.name==\"$domain_name\") | .service_id")

    if [ -z "$domain_id" ]; then
        echo "Domain not found or you do not have permission to delete records."
        exit 1
    fi

    curl -X DELETE "https://min.hostup.se/api/service/$service_id/dns/$domain_id/records/$record_id" \
        -u "$USERNAME:$PASSWORD"
}

# Main logic to call the appropriate function based on the arguments passed
if [ "$#" -eq 0 ]; then
    list_domains
elif [ "$1" == "--help" ]; then
    display_help
elif [ "$1" == "add" ]; then
    if [ "$#" -ne 7 ]; then
        echo "Usage: ./hostup-dns add [domain_name] [record_name] [record_type] [record_content] [record_ttl] [record_priority]"
        exit 1
    fi
    add_dns_record "$2" "$3" "$4" "$5" "$6" "$7"
elif [ "$1" == "update" ]; then
    if [ "$#" -ne 8 ]; then
        echo "Usage: ./hostup-dns update [domain_name] [record_id] [record_name] [record_type] [record_content] [record_ttl] [record_priority]"
        exit 1
    fi
    update_dns_record "$2" "$3" "$4" "$5" "$6" "$7" "$8"
elif [ "$1" == "delete" ]; then
    if [ "$#" -ne 3 ]; then
        echo "Usage: ./hostup-dns delete [domain_name] [record_id]"
        exit 1
    fi
    delete_dns_record "$2" "$3"
elif [ "$#" -eq 1 ]; then
    list_dns_records "$1"
else
    echo "Usage: ./hostup-dns [domain] or ./hostup-dns add|update|delete [arguments]"
    exit 1
fi

Var den här artikeln hjälpsam?

Tack för din feedback!