Skip to content

Instantly share code, notes, and snippets.

@somic somic/
Created Aug 31, 2010

What would you like to do?
Scrape AWS EC2 forums to obtain ranges of public IP addresses for each EC2 region
#!/usr/bin/env python
# - ec2_describe_ipaddress_ranges
from BeautifulSoup import BeautifulSoup
import re, urllib2, socket, boto.ec2
import boto
REGION_NAMES = [ 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 = ""
def ec2_describe_ipaddress_ranges():
f = urllib2.urlopen(ANN_URL)
soup = BeautifulSoup(
data = soup.findAll('div', attrs={ 'class': 'jive-body' })[0]
ranges = { }
for line in str(data).split('\n'):
m ='(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d{1,2})', line)
if not m: continue
cidr =
ip = cidr[:cidr.find('/')-2] + '.1'
hostname = socket.gethostbyaddr(ip)[0]
region = [ reg for reg in REGION_NAMES if
hostname.find(reg) > 0 ][0]
except (socket.herror, IndexError):
# workaround for one prefix that breaks the rule
if cidr == '': region = 'ap-northeast-1'
if region not in ranges: ranges[region] = [ ]
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)

This comment has been minimized.

Copy link

commented Oct 13, 2015


This comment has been minimized.

Copy link

commented Jun 2, 2016

Disclaimer ( I work for Luminati Network),
Luminati is a P2P proxy network with over 40 million exit nodes in every city and country in the word.
Those exit nodes are real devices so that are not recognized as proxy/bot/tor.

Feel free to contact me at or signup for a free trial at:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.