DNS API Powershell Beispiel

Skip to main content
Du bist hier:
Drucken

DNS API Powershell Beispiel

Hier finden Sie Beispiele zur Verwendung der DNS-API mit PowerShell.

Authentifizierung

API-Key verwenden (empfohlen)

Den API-Key finden Sie in Ihrem Kundenprofil unter Account > Kundenprofil > API Zugang. Aktivieren Sie dort den API-Zugang und kopieren Sie den angezeigten API-Key.

$apiKey = "IHR_API_KEY_AUS_WHMCS"
$headers = @{"X-API-Key"=$apiKey; "Content-Type"="application/json"}

Alternative: Basic Auth (nicht empfohlen bei Sonderzeichen im Passwort)

$username = "ihre-email@example.ch"
$password = "ihr-passwort"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$headers = @{Authorization=("Basic {0}" -f $base64AuthInfo); "Content-Type"="application/json"}

Alle Zonen auflisten

$result = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$result.zones | Format-Table id, name

Zone-ID anhand des Domainnamens ermitteln

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

if ($zoneId) {
    Write-Host "Zone-ID für $domain ist: $zoneId"
} else {
    Write-Host "Zone nicht gefunden!"
}

Records einer Zone auflisten

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

$result = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/records"
$result.records | Format-Table name, type, data, ttl

Record hinzufügen

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

$body = @{
    name = "test.$domain"
    type = "A"
    data = "192.168.1.1"
    ttl = 3600
} | ConvertTo-Json

$result = Invoke-RestMethod -Method Post -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body
Write-Host "Record-ID: $($result.record_id)"

Record aktualisieren

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

$body = @{
    name = "test.$domain"
    type = "A"
    old_data = "192.168.1.1"
    data = "192.168.1.2"
    ttl = 3600
} | ConvertTo-Json

Invoke-RestMethod -Method Put -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body

Record löschen

$domain = "example.ch"
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

$body = @{
    name = "test.$domain"
    type = "A"
    data = "192.168.1.2"
} | ConvertTo-Json

Invoke-RestMethod -Method Delete -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body

ACME DNS-01 Challenge (Let’s Encrypt)

Automatische Propagation: Bei _acme-challenge.* Records wartet die API automatisch, bis alle DNS-Server den Eintrag haben. Die Antwort enthält Details zur Propagation.

# Konfiguration - API-Key aus WHMCS Kundenprofil kopieren
$apiKey = "IHR_API_KEY_AUS_WHMCS"
$headers = @{"X-API-Key"=$apiKey; "Content-Type"="application/json"}
$domain = "example.ch"
$token = "ihr-acme-token-hier"

# Zone-ID ermitteln
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id

# ACME Challenge Token hinzufügen
$body = @{
    name = "_acme-challenge.$domain"
    type = "TXT"
    data = "`"$token`""
    ttl = 60
} | ConvertTo-Json

$result = Invoke-RestMethod -Method Post -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body

# Prüfen ob Propagation erfolgreich war
if ($result.propagation.propagated) {
    Write-Host "DNS propagiert in $($result.propagation.time) Sekunden"
    Start-Sleep -Seconds 5
} else {
    Write-Host "Warnung: Propagation nicht vollständig!"
}

# === Hier ACME-Validierung durchführen ===

# Nach Validierung: Token löschen
$body = @{
    name = "_acme-challenge.$domain"
    type = "TXT"
} | ConvertTo-Json

Invoke-RestMethod -Method Delete -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body

Komplettes Skript als Funktion

function Set-FirestormDnsRecord {
    param(
        [string]$ApiKey,
        [string]$Domain,
        [string]$RecordName,
        [string]$RecordType,
        [string]$RecordData,
        [int]$TTL = 3600
    )

    $headers = @{"X-API-Key"=$ApiKey; "Content-Type"="application/json"}

    # Zone-ID ermitteln
    $zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
    $zoneId = ($zones.zones | Where-Object { $_.name -eq $Domain }).id

    if (-not $zoneId) {
        throw "Zone $Domain nicht gefunden!"
    }

    # Record hinzufügen
    $body = @{
        name = $RecordName
        type = $RecordType
        data = $RecordData
        ttl = $TTL
    } | ConvertTo-Json

    return Invoke-RestMethod -Method Post -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zone/$zoneId/record" -Body $body
}

# Verwendung:
Set-FirestormDnsRecord -ApiKey "IHR_API_KEY_AUS_WHMCS" -Domain "example.ch" -RecordName "test.example.ch" -RecordType "A" -RecordData "192.168.1.1"

Hinweis

Bevor Sie die API verwenden können, müssen Sie den API-Zugang im Admin-Panel unter Account > Kundenprofil > API Zugang aktivieren. Nach der Aktivierung wird Ihr persönlicher API-Key angezeigt.

Related Post