#!/usr/bin/env bash # set -x set -e # # Source the environment variables # if [[ -f .env ]]; then source .env elif [[ -f /etc/azure-dns-update/.env ]]; then source /etc/azure-dns-update/.env else #if [[ ! -f .env ]]; then echo "MISSING .env FILE!" exit -1 fi # # Logging # write_log() { logLevel='DEBUG' if [[ ! -z $2 ]]; then echo "$1: $2" else echo "logLevel: $1" fi } write_message() { write_log 'INFO' "$1" } # # 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/$TENANT_ID/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/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/providers/Microsoft.Network/dnsZones/$zone_name/$RECORD_TYPE/$RELATIVE_RECORD_SET_NAME?api-version=2018-05-01 } resource_group_list=($RESOURCE_GROUP_NAMES) zone_list=($ZONE_NAMES) write_message "=======================================" write_message "=======================================" for i in "${!resource_group_list[@]}"; do resource_group="${resource_group_list[i]}" zone_name="${zone_list[i]}" access_token=$(get_token) # Get the Record Set azure_recordset=$(get_recordset) write_message "Azure Record Set: $zone_name" # Parse the Record Set azure_ip_address=$(get_recordset_address $azure_recordset) write_message "Azure IP: $azure_ip_address" # Get the currently assigned Public IP isp_ip_address=$(dig +short myip.opendns.com @resolver1.opendns.com) write_message "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 write_message "IP ADDRESSES DON'T MATCH. UPDATING..." # Prepare the request content azure_recordset_UPDATE=$(update_recordset $isp_ip_address) write_message $azure_recordset_UPDATE # Perform the Update AZURE_PUT_RESPONSE=$(put_recordset $azure_recordset_UPDATE) write_message "Azure PUT Response: $AZURE_PUT_RESPONSE" write_message "=======================================" write_message $'Azure IP Address updated successfully\n' write_message "=======================================" else write_message "=======================================" write_message $'IP Address is up to date.\n' write_message "=======================================" fi done write_message "=======================================" write_message "======================================="