Skip to content

Instantly share code, notes, and snippets.

@ramondeklein
Created March 12, 2024 20:08
Show Gist options
  • Save ramondeklein/826c96cbc9ae3208effc227019912fd0 to your computer and use it in GitHub Desktop.
Save ramondeklein/826c96cbc9ae3208effc227019912fd0 to your computer and use it in GitHub Desktop.
Update Azure DNS based on current external IP (dyndns)
FROM mcr.microsoft.com/azure-cli:latest
COPY azure-update-ddns.sh /usr/local/bin
RUN chmod 755 /usr/local/bin/azure-update-ddns.sh
CMD ["/usr/local/bin/azure-update-ddns.sh"]
#!/bin/bash
[ -z "$AZUREAD_TENANT" ] && (echo "No AZUREAD_TENANT specified" >&2; exit 1)
[ -z "$AZUREAD_APPID" ] && (echo "No AZUREAD_APPID specified" >&2; exit 1)
[ -z "$AZUREAD_SECRET" ] && (echo "No AZUREAD_SECRET specified" >&2; exit 1)
[ -z "$AZURE_RESOURCE_GROUP" ] && (echo "No AZURE_RESOURCE_GROUP specified" >&2; exit 1)
[ -z "$DNS_DOMAIN" ] && (echo "No DNS_DOMAIN specified" >&2; exit 1)
[ -z "$DNS_RECORD" ] && (echo "No DNS_RECORD specified" >&2; exit 1)
echo "Logging in to Azure on tenant $AZUREAD_TENANT with application ID $AZUREAD_APPID"
az login --service-principal --tenant $AZUREAD_TENANT -u $AZUREAD_APPID -p $AZUREAD_SECRET
while true
do
MY_IP=$(wget -qO- https://ifconfig.me)
if [ -z "$MY_IP" ]; then
echo "Unable to determine current IP address (no internet???)"
sleep 60
continue
fi
AZURE_IP=$(az network dns record-set a show --resource-group $AZURE_RESOURCE_GROUP --zone-name $DNS_DOMAIN --name $DNS_RECORD --query 'ARecords[0].ipv4Address' -otsv)
if [ -z "$AZURE_IP" ]; then
echo "Unable to determine Azure IP address"
sleep 60
continue
fi
if [ "$MY_IP" = "$AZURE_IP" ]; then
echo "IP address is $MY_IP and Azure is up-to-date"
else
echo "Changing IP address from $AZURE_IP to $MY_IP"
az network dns record-set a remove-record --resource-group $AZURE_RESOURCE_GROUP --zone-name $DNS_DOMAIN --record-set-name $DNS_RECORD --ipv4-address "$AZURE_IP"
az network dns record-set a add-record --resource-group $AZURE_RESOURCE_GROUP --zone-name $DNS_DOMAIN --record-set-name $DNS_RECORD --ipv4-address "$MY_IP"
fi
sleep 3600
done
version: '3'
services:
update-dns-azure:
image: ramondeklein/azure-update-ddns
build:
context: .
dockerfile: azure-update-ddns.dockerfile
environment:
- AZUREAD_TENANT=<TENANT_ID_HERE>
- AZUREAD_APPID=<APP_ID_HERE>
- AZUREAD_SECRET=<APP_SECRET_HERE>
- AZURE_RESOURCE_GROUP=dns
- DNS_DOMAIN=example.com
- DNS_RECORD=@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment