jean-cloud-services/provisioning/roles/deploy_all/files/bin/resolv.sh
2023-05-02 16:23:37 +02:00

64 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
# This script echo ipv4 addresses of a symbolic name.
# One IP per line
set -euo pipefail
########################### Helpers ###########################################
function yell {
echo "$@" >&2
}
function die {
yell "$@"
exit 1
}
function say {
if "$verbose" ; then
yell "$@"
fi
}
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)"
}
########################### Options ###########################################
verbose=false
if [ "$#" -gt 1 ] && [ "$1" = '-v' ] ; then
verbose=true
shift
fi
########################### arguments ##########################################
if [ "$#" -ne 1 ] ; then
die "Usage: $0 [options] <domain_name>
options : -v verbose"
fi
########################### script ############################################
resolv "$1"