Initial commit — porkbun DDNS for carloselugo.com
Credentials sourced from .ddns-env (gitignored, chmod 600).
This commit is contained in:
Executable
+99
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env bash
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
# porkbun-ddns-test.sh
|
||||
# Version de prueba — solo actualiza git.carloselugo.com
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
|
||||
source /opt/ddns/.ddns-env
|
||||
|
||||
DOMAIN="carloselugo.com"
|
||||
SUBDOMAINS=("git")
|
||||
|
||||
LOG_FILE="/home/netintel/porkbun-ddns-test.log"
|
||||
API_BASE="https://api.porkbun.com/api/json/v3"
|
||||
|
||||
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE" >&2; }
|
||||
|
||||
get_public_ip() {
|
||||
local ip
|
||||
ip=$(curl -4 -sf --max-time 5 https://api.ipify.org) ||
|
||||
ip=$(curl -4 -sf --max-time 5 https://ifconfig.me) ||
|
||||
ip=$(curl -4 -sf --max-time 5 https://icanhazip.com)
|
||||
echo "$ip"
|
||||
}
|
||||
|
||||
get_dns_ip() {
|
||||
local subdomain="$1"
|
||||
local response
|
||||
response=$(curl -4 -sf --max-time 10 \
|
||||
-X POST "$API_BASE/dns/retrieveByNameType/$DOMAIN/A/$subdomain" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"apikey\":\"$API_KEY\",\"secretapikey\":\"$SECRET_KEY\"}")
|
||||
log " [API retrieve] $response"
|
||||
echo "$response" | grep -o '"content":"[^"]*"' | head -1 | cut -d'"' -f4
|
||||
}
|
||||
|
||||
update_record() {
|
||||
local subdomain="$1"
|
||||
local new_ip="$2"
|
||||
# editByNameType — no record ID needed, no risk of accidental create
|
||||
local result
|
||||
result=$(curl -4 -sf --max-time 10 \
|
||||
-X POST "$API_BASE/dns/editByNameType/$DOMAIN/A/$subdomain" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"apikey\":\"$API_KEY\",\"secretapikey\":\"$SECRET_KEY\",\"content\":\"$new_ip\",\"ttl\":\"600\"}")
|
||||
log " [API editByNameType] $result"
|
||||
echo "$result" | grep -o '"status":"[^"]*"' | head -1 | cut -d'"' -f4
|
||||
}
|
||||
|
||||
main() {
|
||||
log "=== PRUEBA INICIADA ==="
|
||||
|
||||
local forced_ip=""
|
||||
while getopts "f:" opt; do
|
||||
case $opt in
|
||||
f) forced_ip="$OPTARG" ;;
|
||||
*) echo "Uso: $0 [-f <ip>]"; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
local current_ip
|
||||
if [ -n "$forced_ip" ]; then
|
||||
current_ip="$forced_ip"
|
||||
log "IP forzada manualmente: $current_ip"
|
||||
else
|
||||
current_ip=$(get_public_ip)
|
||||
if [ -z "$current_ip" ]; then
|
||||
log "ERROR: No se pudo obtener la IP pública."
|
||||
exit 1
|
||||
fi
|
||||
log "IP pública actual: $current_ip"
|
||||
fi
|
||||
|
||||
for subdomain in "${SUBDOMAINS[@]}"; do
|
||||
log "── Procesando: $subdomain ──"
|
||||
|
||||
local dns_ip
|
||||
dns_ip=$(get_dns_ip "$subdomain")
|
||||
log " IP en DNS: ${dns_ip:-'(no encontrada)'}"
|
||||
|
||||
if [ "$current_ip" = "$dns_ip" ]; then
|
||||
log " [$subdomain] Ya está actualizado ($current_ip). No se hace nada."
|
||||
continue
|
||||
fi
|
||||
|
||||
log " [$subdomain] Actualizando: ${dns_ip:-'?'} → $current_ip"
|
||||
local status
|
||||
status=$(update_record "$subdomain" "$current_ip")
|
||||
|
||||
if [ "$status" = "SUCCESS" ]; then
|
||||
log " [$subdomain] ✓ Actualizado correctamente."
|
||||
else
|
||||
log " [$subdomain] ✗ Falló. Status: $status"
|
||||
fi
|
||||
done
|
||||
|
||||
log "=== PRUEBA TERMINADA ==="
|
||||
}
|
||||
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user