Skip to content

Instantly share code, notes, and snippets.

@louiszuckerman
Created June 24, 2019 22:03
Show Gist options
  • Save louiszuckerman/d2cd2bf3b7a4d12dc84b6de07bf960f9 to your computer and use it in GitHub Desktop.
Save louiszuckerman/d2cd2bf3b7a4d12dc84b6de07bf960f9 to your computer and use it in GitHub Desktop.
list all of the IPv4 addresses in an AWS region and provide a summary count
"""
List & count all IPv4 addresses in an AWS region
"""
import requests
REGION = 'us-east-1'
rsp = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json')
rj = rsp.json()
print("Here are all of the IPv4 netblocks in %s" % REGION)
acc = 0
for i in rj['prefixes']:
if i.get('ip_prefix') and i['region'] == REGION:
range = i['ip_prefix']
print(range)
acc += 2 ** (32 - int(range[range.index('/')+1:]))
print("These cover a total of %s IP addresses" % acc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment