Skip to content

Instantly share code, notes, and snippets.

@Gabriellpweb
Created May 4, 2018 20:36
Show Gist options
  • Save Gabriellpweb/d5a4a95cc99ccc93e31db52d160914aa to your computer and use it in GitHub Desktop.
Save Gabriellpweb/d5a4a95cc99ccc93e31db52d160914aa to your computer and use it in GitHub Desktop.
List hosted zones and subdomains of route53
#!/bin/bash
# USAGE: route53subls
#
# OUTPUT:
#
# Domain |SubDomain |RecordType|RecordValue
# website.com. |sub.website.com. |A |dualstack.websitealb-12345678998.sa-east-1.elb.amazonaws.com.
#
zoneids=$(aws route53 list-hosted-zones | jq '.HostedZones[] | "\(.Id)"' | sed -e 's/\"/\s/g' | sed 's/[^0-9|A-Z]//g')
output_file=/tmp/route53subls.out
echo '' > $output_file
for zid in $zoneids;
do
hostedzone=$(aws route53 get-hosted-zone --id $zid | jq '.HostedZone.Name' | sed -e 's/\"//g')
records=$(aws route53 list-resource-record-sets --hosted-zone-id $zid)
numRecords=$(echo $records | jq '.ResourceRecordSets | length')
if [ $numRecords -gt 0 ]
then
for ((c=0; c < $numRecords; c++))
do
record=$(echo $records | jq ".ResourceRecordSets[$c]")
name=$(echo $record | jq ".Name" | sed -e 's/\"//g')
rtype=$(echo $record | jq ".Type" | sed -e 's/\"//g')
rsets=''
numSets=$(echo $record | jq ".ResourceRecords | length")
if [ $numSets -gt 0 ]; then
for ((s=0; s < $numSets;s++))
do
rset=$(echo $record | jq ".ResourceRecords[$s].Value" | sed -e 's/\"//g' | sed -e 's/ /_/g')
echo "$hostedzone $name $rtype $rset" >> $output_file
done
elif [[ $rtype == 'A' ]]; then
rset=$(echo $record | jq ".AliasTarget.DNSName" | sed -e 's/\"//g')
echo "$hostedzone $name $rtype $rset" >> $output_file
fi
done
fi
#break
done
cat $output_file | column -t --table-noextreme 2,4 -N Domain,SubDomain,RecordType,RecordValue -o '|'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment