From 8eab9df256e3f8bda40632b3ea59cb39edad64ff Mon Sep 17 00:00:00 2001 From: Mike Heier Date: Sun, 21 Aug 2022 08:53:57 -0500 Subject: [PATCH] Making log output friendlier --- update-dns.sh | 47 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/update-dns.sh b/update-dns.sh index cfdad76..89223d7 100755 --- a/update-dns.sh +++ b/update-dns.sh @@ -15,7 +15,22 @@ else echo "MISSING .env FILE!" exit -1 fi -#source .env + +# +# 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 @@ -62,12 +77,11 @@ put_recordset() { 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 } -# Get an Accesss Token -#access_token=$(get_token) -# echo $access_token - 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]}" @@ -75,31 +89,38 @@ for i in "${!resource_group_list[@]}"; do # Get the Record Set azure_recordset=$(get_recordset) - echo "Azure Record Set: $azure_recordset" + write_message "Azure Record Set: $zone_name" # Parse the Record Set azure_ip_address=$(get_recordset_address $azure_recordset) - echo "Azure IP: $azure_ip_address" + write_message "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" + 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 - echo "IP ADDRESSES DON'T MATCH. UPDATING..." + write_message "IP ADDRESSES DON'T MATCH. UPDATING..." # Prepare the request content azure_recordset_UPDATE=$(update_recordset $isp_ip_address) - echo $azure_recordset_UPDATE + write_message $azure_recordset_UPDATE # Perform the Update AZURE_PUT_RESPONSE=$(put_recordset $azure_recordset_UPDATE) - echo "Azure PUT Response: $AZURE_PUT_RESPONSE" + write_message "Azure PUT Response: $AZURE_PUT_RESPONSE" - echo "Azure IP Address updated successfully" + write_message "=======================================" + write_message $'Azure IP Address updated successfully\n' + write_message "=======================================" else - echo "IP Address is up to date." + write_message "=======================================" + write_message $'IP Address is up to date.\n' + write_message "=======================================" fi done + +write_message "=======================================" +write_message "======================================="