Skip to content

Instantly share code, notes, and snippets.

@neilkod
Created December 31, 2010 13:29
Show Gist options
  • Save neilkod/761010 to your computer and use it in GitHub Desktop.
Save neilkod/761010 to your computer and use it in GitHub Desktop.
given an IP address, return the EC2 region it originates from
# requires IPy https://github.com/haypo/python-ipy
from IPy import IP
# putting EC2_REGIONS here so the map only has to be performed once.
# I'm not sure if this is a great idea. Comments?
# ideally this list will be scraped from the following source
# https://forums.aws.amazon.com/ann.jspa?annID=857
# thanks to serverfault.com user Flashman for pointing me to this doc.
EC2_REGIONS = { "asia_pacific_singapore" : map(IP,[ "175.41.128.0/18" ]),
"eu_ireland" : map(IP,[ "79.125.0.0/17",
"46.51.128.0/18",
"46.51.192.0/20",
"46.137.0.0/17"
]),
"us_east" : map(IP,[ "216.182.224.0/20",
"72.44.32.0/19",
"67.202.0.0/18",
"67.202.0.0/18",
"75.101.128.0/17",
"174.129.0.0/16",
"204.236.192.0/18",
"184.73.0.0/16",
"184.72.128.0/17",
"184.72.64.0/18",
"50.16.0.0/15"
]),
"us_west" : map(IP,[ "204.236.128.0/18",
"184.72.0.0/18",
"50.18.0.0/18"
])
}
def ec2_region(address):
""" given an ip address, returns the EC2 region, if applicable
otherwise return None """
# ip ranges curated from https://forums.aws.amazon.com/ann.jspa?annID=857
# we might want to make a scraper or a batch job that keeps these current
for key,region in EC2_REGIONS.iteritems():
if any([address in x for x in region]):
return key
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment