powerdns-tools/bin/pdns-search.sh

58 lines
1.5 KiB
Bash
Raw Normal View History

2024-02-02 16:26:32 -08:00
#!/bin/bash
#
# pdns-tools
# https://git.stack-source.com/msb/pdns-tools
# Copyright (c) 2024 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# 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()
{
echo "Search data in Zones, Comments and RRSets."
echo ""
echo "usage: $thisfilename -q <query> [-e <max>] [-o <object>] [-c] [-h]"
echo ""
echo " -h Print this help."
echo " -q <query> The string to search for."
2024-02-06 10:33:49 -08:00
echo " -e <max> Maximum number of entries to return (defaults to 100)."
2024-02-02 16:26:32 -08:00
echo " -o <object> 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
else
echo Unexecpted http response seraching for $query: $zone_status
2024-02-02 16:26:32 -08:00
fi
rm $tmpfile