Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Scrape AWS EC2 forums to obtain ranges of public IP addresses for each EC2 region

View ec2dipr.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#!/usr/bin/env python
#
# ec2dipr.py - ec2_describe_ipaddress_ranges
#
 
from BeautifulSoup import BeautifulSoup
import re, urllib2, socket, boto.ec2
 
try:
import boto
REGION_NAMES = [ reg.name for reg in boto.ec2.regions() ]
except ImportError:
# hard code if boto is not installed
REGION_NAMES = [ "us-east-1", "us-west-1", "eu-west-1", "ap-southeast-1" ]
DEFAULT_REGION = "us-east-1"
 
ANN_URL = "https://forums.aws.amazon.com/ann.jspa?annID=1182"
 
def ec2_describe_ipaddress_ranges():
f = urllib2.urlopen(ANN_URL)
soup = BeautifulSoup(f.read())
f.close()
 
data = soup.findAll('div', attrs={ 'class': 'jive-body' })[0]
ranges = { }
for line in str(data).split('\n'):
m = re.search('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2})', line)
if not m: continue
 
cidr = m.group(0)
ip = cidr[:cidr.find('/')-2] + '.1'
region = DEFAULT_REGION
try:
hostname = socket.gethostbyaddr(ip)[0]
region = [ reg for reg in REGION_NAMES if
hostname.find(reg) > 0 ][0]
except (socket.herror, IndexError):
pass
 
# workaround for one prefix that breaks the rule
if cidr == '46.51.224.0/19': region = 'ap-northeast-1'
 
if region not in ranges: ranges[region] = [ ]
ranges[region].append(cidr)
 
return ranges
 
def region_sizes():
" Count the number of /24 prefixes allocated to each region "
d = { }
for region, ranges in ec2_describe_ipaddress_ranges().iteritems():
d[region] = 0
for range in ranges:
cidr = int(range.split('/')[-1])
print region, cidr
d[region] += 2 ** (24 - cidr)
return d
 
if __name__ == '__main__':
for region, ranges in ec2_describe_ipaddress_ranges().iteritems():
print region, ' '.join(ranges)
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.