Amazon EC2 IP Detector for Python

20. November 2014 Blog 0

The other day I need to be able to detect if an IP address was an Amazon EC2 IP address for a project I was working on. I couldn’t find any code but I did find this: https://forums.aws.amazon.com/ann.jspa?annID=1701 Which is a list of all the ip ranges for Amazon EC2 servers. So first a wrote a parser to parse all these ip ranges. Then I wrote this function in order to detect if a specific ip is in one of those ranges:

def amazon_ip(incoming_ip):
	pieces = incoming_ip.split('.')
	ip2int = ''
	for num in pieces:
		ip2int = ip2int + num.zfill(3)
	ip2int = int(ip2int)

	ranges = [['107020000000','107023255255'],
		['174129000000','174129255255'],
		['184072128000','184072255255'],
		['184072064000','184072127255'],
		['184073000000','184073255255'],
		['204236192000','204236255255'],
		['216182224000','216182239255'],
		['023020000000','023023255255'],
		['050016000000','050017255255'],
		['050019000000','050019255255'],
		['054144000000','054147255255'],
		['054156000000','054159255255'],
		['054160000000','054167255255'],
		['054172000000','054173255255'],
		['054174000000','054175255255'],
		['054196000000','054197255255'],
		['054198000000','054198255255'],
		['054204000000','054205255255'],
		['054208000000','054209255255'],
		['054210000000','054211255255'],
		['054221000000','054221255255'],
		['054224000000','054225255255'],
		['054226000000','054227255255'],
		['054234000000','054235255255'],
		['054236000000','054237255255'],
		['054242000000','054243255255'],
		['054080000000','054087255255'],
		['054088000000','054091255255'],
		['054092128000','054092255255'],
		['067202000000','067202063255'],
		['072044032000','072044063255'],
		['075101128000','075101255255'],
		['050112000000','050112255255'],
		['054245000000','054245255255'],
		['054244000000','054244255255'],
		['054214000000','054214255255'],
		['054212000000','054213255255'],
		['054218000000','054218255255'],
		['054200000000','054201255255'],
		['054202000000','054203255255'],
		['054184000000','054191255255'],
		['054068000000','054071255255'],
		['054148000000','054149255255'],
		['204236128000','204236191255'],
		['184072000000','184072063255'],
		['050018000000','050018255255'],
		['184169128000','184169255255'],
		['054241000000','054241255255'],
		['054215000000','054215255255'],
		['054219000000','054219255255'],
		['054193000000','054193255255'],
		['054176000000','054177255255'],
		['054183000000','054183255255'],
		['054067000000','054067255255'],
		['079125000000','079125127255'],
		['046051128000','046051191255'],
		['046051192000','046051207255'],
		['046137000000','046137127255'],
		['046137128000','046137191255'],
		['176034128000','176034255255'],
		['176034064000','176034127255'],
		['054247000000','054247255255'],
		['054246000000','054246255255'],
		['054228000000','054228255255'],
		['054216000000','054217255255'],
		['054229000000','054229255255'],
		['054220000000','054220255255'],
		['054194000000','054195255255'],
		['054072000000','054075255255'],
		['054076000000','054077255255'],
		['054078000000','054078255255'],
		['054074000000','054075255255'],
		['185048120000','185048123255'],
		['054170000000','054171255255'],
		['054154000000','054155255255'],
		['175041128000','175041191255'],
		['122248192000','122248255255'],
		['046137192000','046137255255'],
		['046051216000','046051223255'],
		['054251000000','054251255255'],
		['054254000000','054254255255'],
		['054255000000','054255255255'],
		['054179000000','054179255255'],
		['054169000000','054169255255'],
		['054151128000','054151255255'],
		['054252000000','054252255255'],
		['054253000000','054253255255'],
		['054206000000','054206255255'],
		['054079000000','054079255255'],
		['054066000000','054066255255'],
		['175041192000','175041255255'],
		['046051224000','046051255255'],
		['176032064000','176032095255'],
		['103004008000','103004015255'],
		['176034000000','176034063255'],
		['054248000000','054249255255'],
		['054250000000','054250255255'],
		['054238000000','054238255255'],
		['054199000000','054199255255'],
		['054178000000','054178255255'],
		['054095000000','054095255255'],
		['054092000000','054092127255'],
		['054168000000','054168255255'],
		['054064000000','054065255255'],
		['054150000000','054150255255'],
		['177071128000','177071255255'],
		['054232000000','054232255255'],
		['054233000000','054233063255'],
		['054207000000','054207255255'],
		['054094000000','054094255255'],
		['054223000000','054223255255'],
		['054093000000','054093255255'],
		['096127000000','096127063255'],
		['096127064000','096127127255']]

	is_amazon = False
	for range in ranges:
		rangeMin = int(range[0])
		rangeMax = int(range[1])
		if(ip2int >= rangeMin and ip2int <= rangeMax):
			#print 'Found in this range'
			#print range
			is_amazon = True

	return is_amazon

Hopefully someone else can find this useful.


Leave a Reply

Your email address will not be published.