Skip to content

Instantly share code, notes, and snippets.

@ericwastaken
Created January 7, 2020 22:07
Show Gist options
  • Save ericwastaken/1a2b7e4dd19ce769031ffd720fc1985d to your computer and use it in GitHub Desktop.
Save ericwastaken/1a2b7e4dd19ce769031ffd720fc1985d to your computer and use it in GitHub Desktop.
#!/bin/bash
################################################################
# This is a script that pulls a list of IPs from AWS
# published IP list, then parses it looking for a
# specific region and service.
#
# Dependencies:
# This script needs:
# * Internet access
# * ./jq installed. jq is a lightweight and flexible
# command-line JSON processor. Available for multiple
# platforms via the public website:
# https://stedolan.github.io/jq/download/
#
# Usage:
# Run the script with no arguments to get syntax.
#
# Release Notes:
# * 2020-01-07 [email protected] - Initial creation.
#
################################################################
# Helper Functions
# Prints the syntax of the command
print-syntax() {
echo ""
echo "Syntax: $0 [aws-region-code] [aws-service-code]"
echo "Valid values: "
echo " aws-region-code: ap-east-1 | ap-northeast-1 | ap-northeast-2 | ap-northeast-3 | ap-south-1 | ap-southeast-1 | ap-southeast-2 | ca-central-1 | cn-north-1 | cn-northwest-1 | eu-central-1 | eu-north-1 | eu-west-1 | eu-west-2 | eu-west-3 | me-south-1 | sa-east-1 | us-east-1 | us-east-2 | us-gov-east-1 | us-gov-west-1 | us-west-1 | us-west-2 | GLOBAL"
echo " aws-service-code: AMAZON | AMAZON_CONNECT | API_GATEWAY | CLOUD9 | CLOUDFRONT | CODEBUILD | DYNAMODB | EC2 | EC2_INSTANCE_CONNECT | GLOBALACCELERATOR | ROUTE53 | ROUTE53_HEALTHCHECKS | S3"
echo "All parameters are case-sensitive."
}
# Output the tool's banner.
echo ""
echo "AWS IP Address Fetch Tool by [email protected]"
# Sanity - we need the region!
if [ -z "$1" ] || [ -z "$2" ]; then
print-syntax
exit
fi
# Set useful variables
AWS_REGION_CODE=$1
AWS_SERVICE_CODE=$2
IP_LOCAL_FILE=ip-ranges.json
CURRENT_PATH=$(pwd)
TIMESTAMP=$(date)
# Pull the current list from AWS using CURL
echo "Retrieving list of IPs from AWS into \"${CURRENT_PATH}/${IP_LOCAL_FILE}\""
curl --silent --output "${CURRENT_PATH}/${IP_LOCAL_FILE}" https://ip-ranges.amazonaws.com/ip-ranges.json
# Extract just the info we need, IP for the passed in region using the jq parser
echo "As of ${TIMESTAMP}, the IP Ranges for the region: \"$AWS_REGION_CODE\" and service: \"$AWS_SERVICE_CODE\" are:"
jq -r ".prefixes[] | select(.region==\"${AWS_REGION_CODE}\" and .service==\"${AWS_SERVICE_CODE}\") | .ip_prefix" < ${CURRENT_PATH}/${IP_LOCAL_FILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment