Skip to content

Instantly share code, notes, and snippets.

@lichti
Last active September 28, 2016 17:49
Show Gist options
  • Save lichti/7a7e0ec162b723c89a1d08f0bee2a197 to your computer and use it in GitHub Desktop.
Save lichti/7a7e0ec162b723c89a1d08f0bee2a197 to your computer and use it in GitHub Desktop.
List all public address from all ec2, elb and eip
#!/bin/bash
ARRAY_ACCESS_KEY="conta001,KEY_ID,ACCESS_KEY:conta002,KEY_ID,ACCESS_KEY"
function ec2-public {
export AWS_DEFAULT_OUTPUT="json"
aws ec2 describe-instances | jq -r --arg ACCOUNT "${1}" --arg REGION "${2}" --arg TYPE "EC2" '.Reservations[] | .Instances | map((.PublicDnsName|tostring), (.PublicIpAddress|tostring), $ACCOUNT, $REGION, $TYPE) | @csv' | grep -v '"","null"'
}
function eip-public {
export AWS_DEFAULT_OUTPUT="json"
aws ec2 describe-addresses | jq -r --arg ACCOUNT "${1}" --arg REGION "${2}" --arg TYPE "EIP" '.Addresses[] | ["", (.PublicIp|tostring), $ACCOUNT, $REGION, $TYPE] | @csv'
}
function elb-public {
export AWS_DEFAULT_OUTPUT="json"
aws elb describe-load-balancers | jq -r --arg ACCOUNT "${1}" --arg REGION "${2}" --arg TYPE "ELB" '.LoadBalancerDescriptions[]| select(.Scheme | contains("internet-facing")) | [(.DNSName|tostring), "", $ACCOUNT, $REGION, $TYPE] | @csv'
}
function FOR_REGIONS {
AWS_REGIONS="sa-east-1,ap-south-1,ap-northeast-2,ap-southeast-2,ap-northeast-1,ap-southeast-1,eu-central-1,eu-west-1,us-west-1,us-west-2,us-east-1"
OIFS=${IFS}
IFS=','
for REGION in ${AWS_REGIONS}
do
export AWS_DEFAULT_REGION="${REGION}"
${1} ${2} ${REGION}
done
IFS=${OIFS}
}
function FOR_ACCESS {
AWS_ACCESS="${1}"
OIFS=${IFS}
IFS=':'
for ACCESS in ${AWS_ACCESS}
do
AWS_ACCOUNT=$(echo "${ACCESS}" | cut -d\, -f1)
export AWS_ACCESS_KEY_ID=$(echo "${ACCESS}" | cut -d\, -f2)
export AWS_SECRET_ACCESS_KEY=$(echo "${ACCESS}" | cut -d\, -f3)
FOR_REGIONS ${2} ${AWS_ACCOUNT}
done
IFS=${OIFS}
}
function RUN {
FOR_ACCESS "${1}" "${2}"
}
echo '"DNS","IP","ACCOUNT","REGION","SERVICE"'
RUN "$ARRAY_ACCESS_KEY" 'eip-public'
RUN "$ARRAY_ACCESS_KEY" 'ec2-public'
RUN "$ARRAY_ACCESS_KEY" 'elb-public'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment