157 lines
3.4 KiB
Bash
Executable File
157 lines
3.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
SCRIPT_PATH=$(realpath -s "${0}")
|
|
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
|
|
SCRIPT=$(basename "$SCRIPT_PATH")
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
Usage: $SCRIPT [OPTIONS]... ZONE[@VIEW]
|
|
|
|
Show DNS zone content.
|
|
|
|
Options:
|
|
-c, --config path to config file
|
|
-h, --help print this help message
|
|
-i, --interactive interactively ask for missing arguments
|
|
-j, --json print json format
|
|
-J, --json-pretty print pretty json format (implies -j)
|
|
-r, --raw print raw format
|
|
-u, --unfiltered also print not supported record types
|
|
|
|
EOF
|
|
exit
|
|
}
|
|
|
|
config_file="$SCRIPT_DIR"/config.sh
|
|
interactive=false
|
|
json=false
|
|
json_pretty=false
|
|
raw=false
|
|
unfiltered=false
|
|
zone=""
|
|
|
|
declare -a args=()
|
|
while [ -n "$1" ]; do
|
|
opt=$1
|
|
shift
|
|
case "$opt" in
|
|
-c|--config)
|
|
if [ -z "$1" ]; then
|
|
echo "$SCRIPT: missing argument to option -- '$opt'" >&2
|
|
exit 1
|
|
fi
|
|
config_file=$1
|
|
shift
|
|
;;
|
|
-h|--help)
|
|
usage
|
|
;;
|
|
-i|--interactive)
|
|
interactive=true
|
|
;;
|
|
-j|--json)
|
|
json=true
|
|
;;
|
|
-J|--json-pretty)
|
|
json=true
|
|
json_pretty=true
|
|
;;
|
|
-r|--raw)
|
|
raw=true
|
|
;;
|
|
-u|--unfiltered)
|
|
unfiltered=true
|
|
;;
|
|
-*)
|
|
echo "$SCRIPT: invalid option -- '$opt'" >&2
|
|
exit 1
|
|
;;
|
|
*)
|
|
args+=("$opt")
|
|
if (( ${#args[@]} > 1 )); then
|
|
echo "$SCRIPT: invalid argument -- '$opt'" >&2
|
|
exit 1
|
|
fi
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if $raw && $json; then
|
|
echo "$SCRIPT: invalid options: --raw and --json are mutually exclusive" >&2
|
|
exit 1
|
|
fi
|
|
|
|
source "$config_file" || exit 2
|
|
|
|
LIB_DIR=${LIB_DIR:-$SCRIPT_DIR/lib}
|
|
source "$LIB_DIR"/dns.sh || exit 3
|
|
source "$LIB_DIR"/output.sh || exit 3
|
|
|
|
set -- "${args[@]}"
|
|
|
|
zone=$1
|
|
if shift; then
|
|
dns_check_zone_view "$zone" zone view || exit 10
|
|
elif $interactive; then
|
|
dns_select_zone zone view || exit 11
|
|
else
|
|
echo "$SCRIPT: missing argument -- ZONE[@VIEW]" >&2
|
|
exit 1
|
|
fi
|
|
|
|
declare -A output
|
|
if [ "${view}" == "*" ]; then
|
|
json_array_to_bash views < <(dns_zone_views "$zone")
|
|
else
|
|
views=("$view")
|
|
fi
|
|
|
|
for view in "${views[@]}"; do
|
|
output["$view"]=$(dns_zone "$zone" "$view" "$unfiltered") || exit 12
|
|
done
|
|
|
|
if $raw; then
|
|
n=0
|
|
for view in $(printf "%s\n" "${!output[@]}" | sort); do
|
|
if (( ${#output[@]} > 1 )) || [ "$view" != "$NAMED_DEFAULT_VIEW" ]; then
|
|
cat <<EOF
|
|
#
|
|
# VIEW: $view
|
|
#
|
|
|
|
EOF
|
|
fi
|
|
echo "${output["$view"]}"
|
|
"$JQ" --raw-output '.[] | "\(.name)\t\(.ttl)\t\(.type)\t\(.value)"' <<<"${output["$view"]}"
|
|
((n++))
|
|
(( $n < ${#output[@]} )) && echo
|
|
done
|
|
exit
|
|
fi
|
|
|
|
if $json; then
|
|
jq_opts=""
|
|
$json_pretty || jq_opts="--compact-output"
|
|
for view in $(printf "%s\n" "${!output[@]}" | sort); do
|
|
jq --compact-output --slurp "{ view: \"$view\", records: .[] }" <<<"${output["$view"]}"
|
|
done | jq --slurp $jq_opts
|
|
exit
|
|
fi
|
|
|
|
max_value_len=$(( ${TERMINAL_WITH} / 2 - 15))
|
|
n=0
|
|
for view in $(printf "%s\n" "${!output[@]}" | sort); do
|
|
(( ${#output[@]} > 1 )) || [ "$view" != "$NAMED_DEFAULT_VIEW" ] && echo "View: $view"
|
|
{
|
|
(( $max_value_len <= 0 )) && max_value_len=1
|
|
while read -r name ttl rtype value; do
|
|
name=$("$IDN2" --decode <<<"$name")
|
|
(( ${#value} > $max_value_len )) && value="${value:0:$max_value_len} ...[TRUNCATED]"
|
|
echo "$name$TAB$ttl$TAB$rtype$TAB$value"
|
|
done < <("$JQ" --raw-output '.[] | "\(.name)\t\(.ttl)\t\(.type)\t\(.value)"' <<<"${output["$view"]}")
|
|
} | table_output "NAME" "TTL" "TYPE" "VALUE"
|
|
((n++))
|
|
(( $n < ${#output[@]} )) && echo
|
|
done
|