Skip to content

Instantly share code, notes, and snippets.

@dennis-f
Created November 23, 2024 13:12
Show Gist options
  • Select an option

  • Save dennis-f/38c1478ec59551273b45228e5141952e to your computer and use it in GitHub Desktop.

Select an option

Save dennis-f/38c1478ec59551273b45228e5141952e to your computer and use it in GitHub Desktop.
This script updates the DNS record for a specified domain using INWX's DynDNS service. It checks the current public IP address and updates the DNS record if the IP has changed. The script uses environment variables for sensitive information and logs all actions.
#!/bin/bash
# Configuration variables
DOMAIN="${INWX_DOMAIN}"
SUBDOMAIN="home"
INWX_USER="${INWX_USERNAME}"
INWX_PASS="${INWX_PASSWORD}"
LOG_FILE="/home/pi/inwx-dyndns_update.log"
# Get current public IP address
CURRENT_IP=$(curl -s https://api.ipify.org)
# Read last known IP address from log file
LAST_IP=$(tail -n 1 "$LOG_FILE" | awk '{print $NF}')
# Check if IP has changed
if [ "$CURRENT_IP" != "$LAST_IP" ]; then
echo "IP has changed. Updating DNS..."
# Call INWX API to update DNS record
UPDATE_RESULT=$(curl -s -u "$INWX_USER:$INWX_PASS" \
"https://dyndns.inwx.com/nic/update?hostname=$SUBDOMAIN.$DOMAIN&myip=$CURRENT_IP")
# Log the result
echo "$(date '+%Y-%m-%d %H:%M:%S') - IP updated: $CURRENT_IP - Result: $UPDATE_RESULT" >> "$LOG_FILE"
else
echo "IP has not changed. No update necessary."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment