jean-cloud-services/provisioning/roles/deploy_all/files/bin/resolv.sh

64 lines
1.2 KiB
Bash
Raw Normal View History

2023-04-24 10:11:09 +00:00
#!/bin/bash
2023-05-02 14:23:37 +00:00
# This script echo ipv4 addresses of a symbolic name.
# One IP per line
2023-04-24 10:11:09 +00:00
set -euo pipefail
########################### Helpers ###########################################
function yell {
echo "$@" >&2
}
function die {
yell "$@"
exit 1
}
function say {
if "$verbose" ; then
yell "$@"
fi
}
2023-05-02 14:23:37 +00:00
function resolv () {
if [ "$#" -ne 1 ] ; then
die "usage: $0 <name>"
fi
name="$1"
say "Querying $name"
while read line ; do
if [[ "$line" = *"is an alias for "* ]] ; then
resolv "$(echo "$line" | cut -d ' ' -f 6)"
elif [[ "$line" = *" has address "* ]] ; then
echo "$line" | cut -d ' ' -f 4
elif [[ "$line" = *" not found: "* ]] ; then
continue
elif [[ "$line" = *" has no A record" ]] ; then
continue
else
say "unmatched: $line"
fi
done <<< "$(host -W 2 -t A "$name" localhost)"
}
2023-04-24 10:11:09 +00:00
########################### Options ###########################################
verbose=false
2023-05-02 14:23:37 +00:00
if [ "$#" -gt 1 ] && [ "$1" = '-v' ] ; then
2023-04-24 10:11:09 +00:00
verbose=true
shift
fi
########################### arguments ##########################################
if [ "$#" -ne 1 ] ; then
die "Usage: $0 [options] <domain_name>
options : -v verbose"
fi
########################### script ############################################
2023-05-02 14:23:37 +00:00
resolv "$1"