DNS API Powershell example
22.07.2022
Skip to main content
DNS API Powershell example
Di seguito troverete esempi per l’utilizzo dell’API DNS con PowerShell.
Autenticazione
Usare la chiave API (consigliato)
Puoi trovare la tua chiave API nel profilo cliente sotto Account > Profilo cliente > Accesso API. Attiva l’accesso API e copia la chiave API visualizzata.
$apiKey = "LA_TUA_CHIAVE_API_DA_WHMCS"
$headers = @{"X-API-Key"=$apiKey; "Content-Type"="application/json"}
Alternativa: Basic Auth (non consigliato con caratteri speciali nella password)
$username = "la-tua-email@example.ch"
$password = "la-tua-password"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
$headers = @{Authorization=("Basic {0}" -f $base64AuthInfo); "Content-Type"="application/json"}
Elencare tutte le zone
$result = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones" $result.zones | Format-Table id, name
Ottenere ID zona per nome dominio
$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 "ID zona per $domain e': $zoneId"
} else {
Write-Host "Zona non trovata!"
}
Elencare i record di una zona
$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
Aggiungere un record
$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 "ID record: $($result.record_id)"
Aggiornare un record
$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
Eliminare un record
$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)
Propagazione automatica: Per i record _acme-challenge.*, l’API attende automaticamente che tutti i server DNS abbiano il record. La risposta contiene i dettagli della propagazione.
# Configurazione - copia la chiave API dal profilo cliente WHMCS
$apiKey = "LA_TUA_CHIAVE_API_DA_WHMCS"
$headers = @{"X-API-Key"=$apiKey; "Content-Type"="application/json"}
$domain = "example.ch"
$token = "il-tuo-token-acme-qui"
# Ottenere ID zona
$zones = Invoke-RestMethod -Headers $headers -Uri "https://api.firestorm.ch/dns/v1/zones"
$zoneId = ($zones.zones | Where-Object { $_.name -eq $domain }).id
# Aggiungere token ACME challenge
$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
# Verificare se la propagazione e' riuscita
if ($result.propagation.propagated) {
Write-Host "DNS propagato in $($result.propagation.time) secondi"
Start-Sleep -Seconds 5
} else {
Write-Host "Attenzione: Propagazione non completa!"
}
# === Eseguire la validazione ACME qui ===
# Dopo la validazione: eliminare il token
$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
Script completo come funzione
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"}
# Ottenere ID zona
$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 "Zona $Domain non trovata!"
}
# Aggiungere record
$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
}
# Utilizzo:
Set-FirestormDnsRecord -ApiKey "LA_TUA_CHIAVE_API_DA_WHMCS" -Domain "example.ch" -RecordName "test.example.ch" -RecordType "A" -RecordData "192.168.1.1"
Nota
Prima di utilizzare l’API, devi attivare l’accesso API nel pannello di amministrazione sotto Account > Profilo cliente > Accesso API. Dopo l’attivazione, verra’ visualizzata la tua chiave API personale.