-
Notifications
You must be signed in to change notification settings - Fork 0
/
hosts
executable file
·66 lines (59 loc) · 2.25 KB
/
hosts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/sh
help() {
#echo "Query multiple hosts at the same time"
#echo "Usage: hosts [-S SERVER] [HOST_OPTIONS] HOST [HOST...]"
local h=hostname
host -h 2>&1 |sed -e 1d -e "2 { s/host /hosts / }" \
-e "3 { s/^/ /; s/ $h.*/ [-S server ] $h [$h...]/ }" \
-e "/^ *-s/ { p; s/s.*/S specifies the DNS server/ }" \
-e "/^ *-v/ { p; s/v.*/V show version and license info/ }"
echo ''
version
}
version() {
echo "Part of net-scripts: https://github.com/adamhotep/net-scripts"
echo "hosts 0.2.20160326.1, Copyright 2002+ by Adam Katz, GPL v2+"
}
# exits with an error if we are missing an option's required argument
check() {
if [ -z "$2" ]; then
echo "hosts: option requires an argument -- $1" >&2
echo "Learn more with: hosts -h" >&2
exit 2
fi
}
if ! command -v host >/dev/null 2>&1; then
echo 'hosts: ERROR - required dependency `host` was not found in your path.'
exit 2
fi
# It's best for wrappers to pass options blindly vs itemizing them with getopts.
# I've added a few long options merely because I can. It'll never be exhaustive.
while [ "$1" != "${1#-}" ]; do
case "$1" in
( -h* | --help ) help; exit ;;
( -V* | --version ) version; exit ;;
( -S | --server ) check "$@"; server="$2"; shift ;;
( -S* ) server="${1#-S}" ;;
( --server=* ) server="${1#--server=}"; check "$1" "$server" ;;
( -- ) shift; break ;;
( --type ) check "$@"; OPTS="$OPTS -t $2"; shift ;;
( --type=* ) OPTS="$OPTS -t ${1#--type=}"; check --type "${1#*=}" ;;
( -[cNtWRm] ) OPTS="$OPTS $1 $2"; shift ;; # all opts requiring args
( -[cNtWRm]* | -? ) OPTS="$OPTS $1" ;; # opt WITH arg or single opt w/o args
( -* )
# Check for clusters of argument-less options ending in S for server.
# Could replace many of the above cases, but this minimizes sed calls.
test="$(echo "x$1" |sed 's/^x-[^cNtWRm]*/-/')"
case "$test" in
( -S ) check "$@"; server="$2"; OPTS="$OPTS ${1%S}"; shift ;;
( -S* ) server="${test#-S}"; OPTS="$OPTS ${1%S$server}" ;;
( * ) OPTS="$OPTS $1" ;;
esac ;;
esac
shift
done
retval=1
for fqdn in "$@"; do
host $OPTS "$fqdn" $server && retval=$?
done
exit $retval # we exit true if ANY query had a result