#!/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 " 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] options : -v verbose" fi ########################### script ############################################ resolv "$1"