routeros-rest-dump-dns-with.../dump-dns-cache.sh
2024-02-02 00:55:45 +02:00

46 lines
846 B
Bash
Executable File

#!/usr/bin/env bash
HOST="$1"
USERNAME="$2"
PASSWORD="$3"
FILTER="$4"
USE_RUBY="0"
if [ -z "${HOST}" ]
then
echo "Missing hostname"
exit 1
fi
if [ -z "${USERNAME}" ]
then
echo "Missing username"
exit 1
fi
if [ -z "${PASSWORD}" ]
then
echo "Missing password"
exit 1
fi
if [ -z "${FILTER}" ]
then
JQ_QUERY_TEMPLATE='.[]'
else
JQ_QUERY_TEMPLATE='.[] | select(.name|contains("###PATTERN###"))'
fi
QUERY='{".proplist": [".id","name","type","data","ttl"]}'
if [ "${USE_RUBY}" -gt "0" ]
then
JQ_QUERY=$( echo "${JQ_QUERY_TEMPLATE}" |ruby replace-str.rb "###PATTERN###" "${FILTER}" )
else
JQ_QUERY=$( echo "${JQ_QUERY_TEMPLATE}" | sed -e "s@###PATTERN###@${FILTER}@g" )
fi
curl -s -k -u "${USERNAME}:${PASSWORD}" "https://${HOST}/rest/ip/dns/cache/print" \
--data "${QUERY}" -H "content-type: application/json" | \
jq "${JQ_QUERY}"