Created a script to keep Azure DNS pointed to the public IP

This commit is contained in:
Mike Heier 2022-07-13 20:39:22 -04:00
commit 4e12581dea
3 changed files with 102 additions and 0 deletions

9
.env-sample Normal file
View File

@ -0,0 +1,9 @@
SUBSCRIPTION_ID=<YOUR_SUBSCRIPTION_ID_GUID>
CLIENT_ID=<YOUR_CLIENT_ID_GUID>
CLIENT_SECRET=<YOUR_CLIENT_SECRET>
TENANT_ID=<YOUR_TENANT_ID_GUID>
RESOURCE_GROUP_NAME=<YOUR_RESOURCE_GROUP_NAME>
ZONE_NAME=<YOUR_DNS_ZONE_NAME>
RECORD_TYPE=A
RELATIVE_RECORD_SET_NAME=@

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env

92
update-dns.sh Executable file
View File

@ -0,0 +1,92 @@
#!/usr/bin/env bash
# set -x
set -e
#
# Source the environment variables
#
if [[ ! -f .env ]]; then
echo "MISSING .env FILE!"
exit -1
fi
source .env
#
# CURL to get the bearer token for the following calls
#
get_token() {
curl -s -X POST \
-d "grant_type=client_credentials&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&resource=https%3A%2F%2Fmanagement.azure.com%2F" \
https://login.microsoftonline.com/9da185bf-c68b-4143-af97-f6744a41c9db/oauth2/token | jq --raw-output '.access_token'
}
#
# CURL to get the recordset
#
get_recordset() {
curl -s -X GET \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP_NAME/providers/Microsoft.Network/dnsZones/$ZONE_NAME/$RECORD_TYPE/$RELATIVE_RECORD_SET_NAME?api-version=2018-05-01 \
| jq '.'
}
#
# Parses the IPv4 IP Address from the Record Set response
#
get_recordset_address() {
get_recordset | jq --raw-output '.properties.ARecords[].ipv4Address'
}
#
# Updates and formats the record set to be used in the PUT request body
#
update_recordset() {
echo $AZURE_RECORDSET | jq -r ".properties.ARecords[].ipv4Address = \"$1\" | del(.name,.type,.id,.properties.fqdn,.properties.provisioningState) | @text"
}
#
# CURL to PUT the updated record set
#
put_recordset() {
curl -s -X PUT \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
--data-raw "$1" \
https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP_NAME/providers/Microsoft.Network/dnsZones/$ZONE_NAME/$RECORD_TYPE/$RELATIVE_RECORD_SET_NAME?api-version=2018-05-01
}
# Get an Accesss Token
ACCESS_TOKEN=$(get_token)
# echo $ACCESS_TOKEN
# Get the Record Set
AZURE_RECORDSET=$(get_recordset)
echo "Azure Record Set: $AZURE_RECORDSET"
# Parse the Record Set
AZURE_IP_ADDRESS=$(get_recordset_address)
echo "Azure IP: $AZURE_IP_ADDRESS"
# Get the currently assigned Public IP
ISP_IP_ADDRESS=$(dig +short myip.opendns.com @resolver1.opendns.com)
echo "ISP IP: $ISP_IP_ADDRESS"
# If the currently assigned public IP doesn't match the configured IP address in Azure...
# update the Azure record set.
if [ $AZURE_IP_ADDRESS != $ISP_IP_ADDRESS ]; then
echo "IP ADDRESSES DON'T MATCH. UPDATING..."
# Prepare the request content
AZURE_RECORDSET_UPDATE=$(update_recordset $ISP_IP_ADDRESS)
echo $AZURE_RECORDSET_UPDATE
# Perform the Update
AZURE_PUT_RESPONSE=$(put_recordset $AZURE_RECORDSET_UPDATE)
echo "Azure PUT Response: $AZURE_PUT_RESPONSE"
echo "Azure IP Address updated successfully"
else
echo "IP Address is up to date."
fi