diff --git a/bin/pdns-search.sh b/bin/pdns-search.sh new file mode 100755 index 0000000..20fa0fa --- /dev/null +++ b/bin/pdns-search.sh @@ -0,0 +1,61 @@ +#!/bin/bash +# +# pdns-tools +# https://git.stack-source.com/msb/pdns-tools +# Copyright (c) 2024 Matthew Saunders Brown +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# load include file +source $(dirname $0)/pdns.sh + +help() +{ + thisfilename=$(basename -- "$0") + echo "$thisfilename" + echo "Search data in Zones, Comments and RRSets." + echo "" + echo "usage: $thisfilename -q [-e ] [-o ] [-c] [-h]" + echo "" + echo " -h Print this help." + echo " -q The string to search for." + echo " -e Maximum number of entries to return." + echo " -o Type of data to search for, one of 'all' (default), 'zone', 'record', 'comment'." + echo " -c Output in csv format instead of columns." +} + +pdns:getoptions "$@" + +# check for query +if [[ -z $query ]]; then + echo "query is required" + exit +fi + +if [[ -n $max ]]; then + query="$query&max=$max" +fi + +if [[ -n $object ]]; then + query="$query&object_type=$object" +fi + +tmpfile=$(mktemp) + +# export zone and check http status +zone_status=$(/usr/bin/curl --silent --output "$tmpfile" --write-out "%{http_code}" -H "X-API-Key: $api_key" $api_base_url/search-data?q=$query) + +if [[ $zone_status = 200 ]]; then + if [[ -n $csv ]]; then + # csv output + dasel -r json -w csv -f $tmpfile|sed -e 's/"""/"/g' + else + # column output + dasel -r json -w csv -f $tmpfile|sed -e 's/"""/"/g'|column -t -s ',' + fi +elif [[ $zone_status = 404 ]]; then + echo 404 Not Found, $zone does not exist +else + echo Unexecpted http response checking for existence of zone $zone: $zone_status +fi + +rm $tmpfile