powerdns-tools/bin/pdns-zone-exp.sh

60 lines
1.4 KiB
Bash
Raw Normal View History

2022-01-21 13:38:32 -08:00
#!/bin/bash
#
# pdns-tools
# https://git.stack-source.com/msb/pdns-tools
2022-08-22 13:45:17 -07:00
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2022-01-21 13:38:32 -08:00
# load include file
source $(dirname $0)/pdns.sh
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
2022-01-29 11:57:10 -08:00
echo "Export full DNS zone in AXFR format."
2022-01-21 13:38:32 -08:00
echo ""
echo "usage: $thisfilename -z <zone> [-h]"
echo ""
echo " -h Print this help."
2022-01-28 15:22:58 -08:00
echo " -z <zone> Zone to export."
2024-02-06 10:33:18 -08:00
echo " -c Output in csv format instead of columns."
2022-01-21 13:38:32 -08:00
}
pdns:getoptions "$@"
# check for zone
if [[ -z $zone ]]; then
echo "zone is required"
exit
fi
2022-01-29 11:54:12 -08:00
tmpfile=$(mktemp)
2024-02-06 10:33:18 -08:00
echo "name,ttl,class,type,value" > $tmpfile.csv
2022-01-21 13:38:32 -08:00
# export zone and check http status
2022-01-29 11:54:12 -08:00
zone_status=$(/usr/bin/curl --silent --output "$tmpfile" --write-out "%{http_code}" -H "X-API-Key: $api_key" $api_base_url/zones/$zone/export)
2022-01-21 13:38:32 -08:00
if [[ $zone_status = 200 ]]; then
2024-02-06 10:33:18 -08:00
# convert tabs to commas
sed -i 's/\t/,/g' $tmpfile
# zone level records
grep ^$zone. $tmpfile >> $tmpfile.csv
# subdomain records
grep -v ^$zone. $tmpfile >> $tmpfile.csv
if [[ $csv ]]; then
cat $tmpfile.csv
else
column -t -s , $tmpfile.csv
fi
2022-01-21 13:38:32 -08:00
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
2024-02-06 10:33:18 -08:00
rm $tmpfile.csv
2022-01-29 11:54:12 -08:00
rm $tmpfile