Skip to content

Instantly share code, notes, and snippets.

@brianwhigham
Created May 24, 2016 16:54
Show Gist options
  • Save brianwhigham/d3d74c17c423254db2ba973f97fae614 to your computer and use it in GitHub Desktop.
Save brianwhigham/d3d74c17c423254db2ba973f97fae614 to your computer and use it in GitHub Desktop.
parse the list of AWS service IPs and filter given a list of regions and or services
#!/usr/bin/python
import json
import sys, getopt
ip_ranges_file_name = None
regions = None
services = None
try:
opts, args = getopt.getopt(sys.argv[1:],"hf:r:s:",["file=","regions=","services=","help"])
except getopt.GetoptError:
print 'awsiprangesfilter [--file <inputfile>] [--regions region1,...] [--services service1,...]'
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', "--help"):
print 'awsiprangesfilter [--file <inputfile>] [--regions region1,...] [--services service1,...]'
sys.exit()
elif opt in ("-f", "--file"):
ip_ranges_file_name = arg
elif opt in ("-r", "--regions"):
regions = arg.split(',')
elif opt in ("-s", "--services"):
services = arg.split(',')
if ip_ranges_file_name is None:
ip_ranges_file_name = '/tmp/ip-ranges.json'
with open(ip_ranges_file_name) as data_file:
ranges = json.load(data_file)
for r in ranges["prefixes"]:
if regions and r["region"] in regions:
if services and r["service"] in services:
print r["ip_prefix"]
if not services:
print r["ip_prefix"]
if not regions:
if services and r["service"] in services:
print r["ip_prefix"]
if not services:
print r["ip_prefix"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment