Initial commit of source
This commit is contained in:
152
dns-record-add
Executable file
152
dns-record-add
Executable file
@@ -0,0 +1,152 @@
|
||||
#!/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] NAME TTL TYPE VALUE
|
||||
|
||||
Add a new record to a DNS zone.
|
||||
|
||||
Options:
|
||||
-c, --config path to config file
|
||||
-h, --help print this help message
|
||||
-f, --force add record without confirmation prompt
|
||||
-i, --interactive interactively ask for missing arguments
|
||||
EOF
|
||||
exit
|
||||
}
|
||||
|
||||
config_file="$SCRIPT_DIR"/config.sh
|
||||
force=false
|
||||
interactive=false
|
||||
|
||||
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
|
||||
;;
|
||||
-f|--force)
|
||||
force=true
|
||||
;;
|
||||
-i|--interactive)
|
||||
interactive=true
|
||||
;;
|
||||
-*)
|
||||
echo "$SCRIPT: invalid option -- '$opt'" >&2
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
args+=("$opt")
|
||||
if (( ${#args[@]} > 6 )); then
|
||||
echo "$SCRIPT: invalid argument -- '$opt'" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
source "$config_file" || exit 2
|
||||
|
||||
LIB_DIR=${LIB_DIR:-$SCRIPT_DIR/lib}
|
||||
source "$LIB_DIR"/dns.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
|
||||
|
||||
name=$1
|
||||
if shift; then
|
||||
dns_check_record_name "$name" name || exit 21
|
||||
elif $interactive; then
|
||||
dns_read_record_name name || exit 10
|
||||
else
|
||||
echo "$SCRIPT: missing argument -- NAME" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ttl=$1
|
||||
if shift; then
|
||||
dns_check_ttl "$ttl" || exit 22
|
||||
elif $interactive; then
|
||||
dns_read_ttl ttl || exit 10
|
||||
else
|
||||
echo "$SCRIPT: missing argument -- TTL" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
rtype=$1
|
||||
if shift; then
|
||||
dns_check_record_type "$rtype" || exit 23
|
||||
elif $interactive; then
|
||||
dns_select_record_type rtype || exit 10
|
||||
else
|
||||
echo "$SCRIPT: missing argument -- TYPE" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
value=$1
|
||||
if shift; then
|
||||
dns_check_record_value "$rtype" "$value" value || exit 24
|
||||
elif $interactive; then
|
||||
dns_read_record_value "$rtype" value || exit 10
|
||||
else
|
||||
echo "$SCRIPT: missing argument -- VALUE" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ "${view}" == "*" ]; then
|
||||
json_array_to_bash views < <(dns_zone_views "$zone")
|
||||
else
|
||||
views=("$view")
|
||||
fi
|
||||
|
||||
if ! $force; then
|
||||
for view in "${views[@]}"; do
|
||||
echo "View: $view"
|
||||
output=$(dns_record_add "true" "$zone" "$view" "$name" "$ttl" "$rtype" "$value" 2>&1)
|
||||
if (( $? == 0 )); then
|
||||
echo -n -e "\e[32m+ $TAB"
|
||||
echo -n -e "$output\e[0m" | grep --color=never -v -E '^(Outgoing update query:|;.*)?$'
|
||||
else
|
||||
echo -e "\e[31mERROR:\n" >&2
|
||||
echo -e "$output\e[0m" >&2
|
||||
exit 30
|
||||
fi
|
||||
done
|
||||
echo
|
||||
! yes_no "Proceed?" && echo -e "Aborted" && exit
|
||||
echo
|
||||
fi
|
||||
|
||||
echo -n "Sending DDNS update(s)... "
|
||||
for view in "${views[@]}"; do
|
||||
output=$(dns_record_add "false" "$zone" "$view" "$name" "$ttl" "$rtype" "$value" 2>&1)
|
||||
if (( $? != 0 )); then
|
||||
echo -e "ERROR\n" >&2
|
||||
echo "$output" >&2
|
||||
exit 31
|
||||
fi
|
||||
done
|
||||
echo "OK"
|
||||
Reference in New Issue
Block a user