-
Notifications
You must be signed in to change notification settings - Fork 3
/
hosts-yaml-to-aaaa
executable file
·85 lines (74 loc) · 2.86 KB
/
hosts-yaml-to-aaaa
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
79
80
81
82
83
84
85
#!/bin/bash -e
default_path="pillar/domain/qe_nue2_suse_org/hosts.yaml"
default_prefix="2a07:de40:a102:5:"
default_dns_base=salt/profile/dns/files/
usage() {
cat << EOF
Usage: hosts-yaml-to-aaaa [path]
Parse a hosts.yaml file with a format as used within the SUSE infrastructure
recording the hostname and mac address of hosts and outputs SLAAC-compliant
AAAA records that point to IPv6 address that the hosts possibly use.
Options:
-v, --verbose execute with verbose output
-h, -?, --help display this help
-o, --offline Output also host entries for hosts that are
offline or do not respond to a ping request.
-p <path>, --path <path> set different <path> to a hosts.yaml file,
by default '$default_path'
-P <P>, --prefix <P> IPv6 address prefix, default '$default_prefix'
-u, --unique Output no already existing AAAA records if found
in <dns_path>.
-d <dns_path>,
--dns-path <dns_path> set different path <dns_path> to DNS zone file,
by default
'$default_dns_base<top_domain>/dns-<domain>'
with '<top_domain>'+'<domain>' derived from <path>.
-r, --reverse Instead of AAAA output reverse PTR records
Examples:
hosts-yaml-to-aaaa pillar/domain/my_org/hosts.yaml
hosts-yaml-to-aaaa -unique --offline --path hosts.yaml
EOF
exit
}
set -o pipefail
prefix="${prefix:-"$default_prefix"}"
unique="${unique:-false}"
offline="${offline:-false}"
opts=$(getopt -o vhuop:P:d:r --long verbose,help,unique,offline,path:,prefix:,dns_path:,reverse -n 'parse-options' -- "$@") || usage
eval set -- "$opts"
while true; do
case "$1" in
-v | --verbose ) set -x; shift ;;
-h | --help ) usage; shift ;;
-u | --unique ) unique=true; shift ;;
-o | --offline ) offline=true; shift ;;
-p | --path ) path="$2"; shift 2 ;;
-P | --prefix ) prefix="$2"; shift 2 ;;
-d | --dns-path ) dns_path="$2"; shift 2 ;;
-r | --reverse ) reverse=true; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
path="${path:-${1:-$default_path}}"
base="$(basename "$(dirname "$path")")"
domain="${base//_/.}"
default_dns_path="salt/profile/dns/files/${base#*_}/dns-$domain"
dns_path="${dns_path:-$default_dns_path}"
export prefix
yq -r 'to_entries[] | [.key, .value.mac] | @tsv' "$path" | while read -r host mac; do
ip=$(mac-to-ipv6 "$mac")
ip=${ip,,}
if [ "$unique" = "true" ]; then
grep -q "$ip" "$dns_path" && continue
fi
if [ "$offline" != "true" ]; then
ping -q -c1 "$ip" >/dev/null || continue
fi
if [ "$reverse" = "true" ]; then
sipcalc -r "$ip" | sed -n "s/ip6\.arpa\./& 14400 PTR $host.$domain./p"
continue
else
echo "$host AAAA $ip"
fi
done