-
Notifications
You must be signed in to change notification settings - Fork 13
/
rlisthosts
executable file
·78 lines (66 loc) · 1.58 KB
/
rlisthosts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
# rlisthosts -- helper for rdo/rup to discover hosts
#
# Might use LDAP one day, but for now it"s just a static list (instead of
# having that list hardcoded in rup, then copied to other tools).
. lib.bash || exit
usage() {
echo "Usage: ${0##*/} [+]<host|@group>..."
echo
echo "Produce a list of hostnames, expanding '@group' specifications."
echo "Comma-separated items are accepted and expanded to space-separated."
echo
echo_opt "host, @grp" "add single host or the specified group"
echo_opt "+host, +@grp" "append to default list, instead of replacing"
echo_opt "-a" "add all hosts from all groups"
echo_opt "-l" "list known groups and exit"
}
declare -A groups=()
groups[default]="wolke sky star land ember wind dust"
if [[ -e ~/.config/rlisthosts ]]; then
while read -r alias rest; do
if [[ $alias == [!#]* ]]; then
groups[$alias]=$rest
fi
done < ~/.config/rlisthosts
fi
hosts=
set -f
set -- ${*//,/ }
set +f
(( $# )) || set -- @default
for arg; do
if [[ $arg == +* ]]; then
arg=${arg#+}
hosts=${groups[default]}
fi
case $arg in
--help)
usage; exit 0;;
-a)
hosts+=" ${groups[*]}";;
-l)
for group in "${!groups[@]}"; do
echo "$group = ${groups[$group]}"
done; exit 0;;
-*)
vmsg "Unknown option '$arg'" >&2; exit 2;;
@all)
hosts+=" ${groups[*]}";;
@*)
if [[ ! ${groups[${arg#@}]} ]]; then
vmsg "Unknown group '$arg'" >&2; exit 1
fi
hosts+=" ${groups[${arg#@}]}";;
*)
hosts+=" $arg";;
esac
done
set -f
hosts=${hosts//,/ }
declare -A seen=()
for host in $hosts; do
seen[$host]=1
done
echo ${!seen[@]}
set +f