Last active
May 25, 2020 11:48
-
-
Save johananl/502adf3e4a3284c3fdfb4277910662ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # wait-for-dns - a script which waits for the availability of a DNS record. | |
| # | |
| # This script is useful in cases where it is necessary to ensure a certain DNS record is resolvable | |
| # without "contaminating" DNS resolvers with cached NXDOMAIN responses. | |
| # | |
| # This script accepts a DNS zone and a record. It then figures out the authoritative nameservers | |
| # for for the zone and queries each of them for the existence of the provided record. If the record | |
| # exists on all of the nameservers, the script exits successfully. If the record doesn't exist on | |
| # any of the nameservers by the time the maximum attempts counter is reached, the script exits with | |
| # an error. | |
| #!/bin/bash | |
| set -eou pipefail | |
| if [[ $# -ne 3 ]]; then | |
| echo "Usage: $0 <zone> <record> <max_attempts>" | |
| exit 1 | |
| fi | |
| zone=$1 | |
| record=$2 | |
| max_attempts=$3 | |
| echo "Figuring out the nameservers for $zone" | |
| nameservers="" | |
| counter=0 | |
| while [[ $counter -lt $max_attempts ]]; do | |
| out=$(dig +short +timeout=2 $zone ns) | |
| ret=$? | |
| if [[ $ret -eq 0 && "$out" != "" ]]; then | |
| nameservers=$out | |
| break | |
| fi | |
| echo "Not available yet" | |
| sleep 1 | |
| counter=$[$counter+1] | |
| done | |
| [[ "$nameservers" != "" ]] || (echo "Could not resolve nameservers for $zone"; exit 1) | |
| for ns in $nameservers; do | |
| echo "Polling $ns for $record.$zone..." | |
| counter=0 | |
| ok=false | |
| while [[ $counter -lt $max_attempts ]] ; do | |
| out=$(dig +short +timeout=2 @$ns $record.$zone a) | |
| ret=$? | |
| if [[ $ret -eq 0 && "$out" != "" ]]; then | |
| echo "Looks good!" | |
| ok=true | |
| break | |
| fi | |
| echo "Not available yet" | |
| sleep 1 | |
| counter=$[$counter+1] | |
| done | |
| if ! $ok; then | |
| echo "$record.$zone didn't become available within the allowed time" | |
| exit 1 | |
| fi | |
| done | |
| echo "$record.$zone is available on all nameservers!" | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment