Skip to content

Instantly share code, notes, and snippets.

@ExZyle
Last active September 25, 2023 04:07
Show Gist options
  • Save ExZyle/a72820ec4a3c4fb7c81c086e7596e31c to your computer and use it in GitHub Desktop.
Save ExZyle/a72820ec4a3c4fb7c81c086e7596e31c to your computer and use it in GitHub Desktop.
Dumps into TSV format all the DNS records for all the zones across all the accounts configured by aws-vault. Prerequisites: awscli (all profiles configured), aws-vault, jq.
#!/bin/bash
#
# Prerequisites: awscli (all profiles configured), aws-vault, jq
#
# Usage: dumpdns.sh
# Trap ctrl-c user interrupt and exit cleanly
trap ctrl_c INT
ctrl_c() {
echo "Cancelled by user" >&2
exit 1
}
#######################################
# Get a list of the AWS profiles
# configured in aws-vault.
# Globals:
# None
# Arguments:
# None
#######################################
get_aws_profiles() {
aws-vault list | grep -e "^\w" | tail -n +2 | awk '{print $1}' #| sort
}
#######################################
# Get the hosted zones for a given
# AWS profile.
# Globals:
# None
# Arguments:
# $1 - AWS profile
#######################################
get_hosted_zones() {
local profile
profile=$1
aws-vault exec $profile -- aws route53 list-hosted-zones \
| jq -r '.HostedZones[] | '\"${profile}\\t\"' + .Id + "\t" + .Name + "\t" + (.Config.PrivateZone | tostring)'
}
#######################################
# Get the records for a hosted zone
# for a given AWS profile and zone ID.
# Globals:
# None
# Arguments:
# $1 - AWS profile
# $2 - Hosted zone ID
#######################################
get_records() {
local profile
local zone_id
profile=$1
zone_id=$2
aws-vault exec $profile -- aws route53 list-resource-record-sets \
--hosted-zone-id $zone_id \
| jq -r '.ResourceRecordSets[] | '\"${profile}\\t${zone_id}\\t\"' + .Name + "\t" + .Type + "\t" + (.TTL | tostring) + "\t" + (.ResourceRecords | tostring)'
}
#######################################
# The main loop
# Globals:
# None
# Arguments:
# None
#######################################
main() {
for profile in $(get_aws_profiles); do
echo "Checking hosted zones for $profile" >&2
local hosted_zones
hosted_zones=$(get_hosted_zones "${profile}")
# Iterate each hosted_zone
while read -r hosted_zone; do
local zone_id
zone_id=$(echo $hosted_zone | awk '{print $2}')
# Skip if there is no zone ID
if [ -z "$zone_id" ]; then
continue
fi
echo "Checking records for $profile $zone_id" >&2
get_records "${profile}" "${zone_id}"
done <<< "$hosted_zones"
done
# get_records "prod" "/hostedzone/Z1J4GJOL7NNHX5"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment