~ wait_for_dns.sh "google.com foobar.foo" Wed 15 Mar 2023 10:49:37 GMT
### Trying to resolve google.com foobar.foo ###
>> Waiting for dns 'google.com' to be available -> 172.217.16.238 2a00:1450:4009:821::200e π
>> Waiting for dns 'foobar.foo' to be available ----> Timed out after 4s π
Last active
March 15, 2023 10:51
-
-
Save luispabon/b390b5dc3184f9a5f8163c2c2ecfcdbb to your computer and use it in GitHub Desktop.
Wait for DNS
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 | |
set -eu | |
hosts=$1 | |
lookup_retries=4 | |
lookup_pause=2 | |
echo -e "\n ### Trying to resolve $hosts ###" | |
for host in $hosts; do | |
echo -n " >> Waiting for dns '$host' to be available " | |
ip_addr="" | |
counter=0 | |
while [ -z "${ip_addr}" ] && [ "${counter}" -lt "${lookup_retries}" ]; do | |
echo -n "-" | |
ip_addr=$(nslookup "$host" | awk '/^Address: / { print $2 }' | tr '\n' ' ') | |
[ -z "${ip_addr}" ] && sleep "${lookup_pause}" | |
counter=$((counter+1)) | |
done | |
if [ -z "${ip_addr}" ]; then | |
echo "> Timed out π" | |
exit 1 | |
else | |
echo "> ${ip_addr}π" | |
fi | |
done | |
echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment