Skip to content

Instantly share code, notes, and snippets.

@stuartleeks
Created November 13, 2020 11:15
Show Gist options
  • Select an option

  • Save stuartleeks/84ed1560694769d404b8b7ec4f0084d5 to your computer and use it in GitHub Desktop.

Select an option

Save stuartleeks/84ed1560694769d404b8b7ec4f0084d5 to your computer and use it in GitHub Desktop.

Revisions

  1. stuartleeks created this gist Nov 13, 2020.
    85 changes: 85 additions & 0 deletions wait-for-dns.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    #!/bin/bash

    ############################################################################################
    #
    # Script used testing access to an Azure storage account with a private endpoint
    #
    ############################################################################################

    #
    # Argument parsing
    #

    function show_usage() {
    echo "wait-for-dns.sh"
    echo
    echo "Wait for DNS name to be resolvable to an IP address"
    echo
    echo -e "\t--name\t(Required) The DNS name to query"
    }

    # Parameter defaults:
    DNS_NAME=""
    # Process switches:
    while [[ $# -gt 0 ]]
    do
    case "$1" in
    --name)
    DNS_NAME=$2
    shift 2
    ;;
    *)
    echo "Unexpected '$1'"
    show_usage
    exit 1
    ;;
    esac
    done

    echo $DNS_NAME
    if [[ -z "$DNS_NAME" ]]; then
    show_usage
    exit 1
    fi

    # TODO - pass this into the script for clarity!
    PUBLIC_DNS_NAME=$(echo $DNS_NAME | sed "s/privatelink\.//")
    STORAGE_ACCOUNT_NAME=$(echo $DNS_NAME | sed "s/\..*//")

    #
    # Lookup the IP address...
    #
    echo "Looking up IP address for $DNS_NAME"
    echo "(using public name: $PUBLIC_DNS_NAME)"
    echo "(using storage account name: $STORAGE_ACCOUNT_NAME)"
    for i in {1..24}; do
    echo "Attempt $i..."
    PRIVATE_IP_ADDRESS=$(host -t A $DNS_NAME | awk '/has address/{print $NF; exit}');
    PUBLIC_IP_ADDRESS=$(host -t A $PUBLIC_DNS_NAME | awk '/has address/{print $NF; exit}');
    if [[ -n $PRIVATE_IP_ADDRESS ]]; then
    echo "Got IP Address $PRIVATE_IP_ADDRESS for name $DNS_NAME"
    echo "Got IP Address $PUBLIC_IP_ADDRESS for name $PUBLIC_DNS_NAME"

    if [[ "$PRIVATE_IP_ADDRESS" == "$PUBLIC_IP_ADDRESS" ]]; then
    echo "Addresses match - done."
    echo "Test az container list..."
    echo "'$STORAGE_ACCOUNT_NAME'"
    AZ_RESULT=$(az storage container list --account-name $STORAGE_ACCOUNT_NAME > /dev/null; echo $?)
    echo "result: '$AZ_RESULT'"
    if [[ "$AZ_RESULT" == "0" ]]; then
    echo "az cli test passed - done!"
    exit 0
    else
    echo "az cli test failed"
    fi
    else
    echo "Addresses don't match"
    fi
    else
    echo "Didn't get private IP address"
    fi
    sleep 5s
    done

    echo "Failed to get IP address for $DNS_NAME"
    exit 1