Last active
October 24, 2021 04:40
-
-
Save franzramadhan/6bbb76632e0ab2912cd1bc8d352de02e to your computer and use it in GitHub Desktop.
Fetch AWS IP ranges for particular region
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get AWS IP address ranges | |
# Reference https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html | |
import urllib3 | |
import logging | |
import json | |
import argparse | |
log = logging.getLogger(__name__) | |
class IpRanges: | |
url = "https://ip-ranges.amazonaws.com/ip-ranges.json" | |
def __init__(self, region, type="ipv4"): | |
self.type = type | |
self.region = region | |
@property | |
def type(self): | |
return self._type | |
@type.setter | |
def type(self, value): | |
allowed_types = ["ipv4", "ipv6"] | |
if value not in allowed_types and not isinstance(value, str): | |
raise ValueError("Type should be either ipv4 or ipv6") | |
self._type = value | |
@property | |
def region(self): | |
return self._region | |
@region.setter | |
def region(self, value): | |
if not isinstance(value, str): | |
raise TypeError("Region should be always in string") | |
self._region = value | |
@staticmethod | |
def request(url, method: None): | |
log.info('Making request to %s', url) | |
try: | |
http = urllib3.PoolManager() | |
response = http.request(method, url) | |
body = response.data | |
try: | |
return json.loads(body) | |
except ValueError: | |
return body | |
except urllib3.exceptions.HTTPError as e: | |
log.error("Request failed: %s", e) | |
except urllib3.exceptions.NewConnectionError as e: | |
log.error("Server connection failed: %s", e) | |
def getPrefixes(self) -> dict: | |
prefixes = {} | |
resp = self.request(self.url, "GET") | |
if self.type == "ipv4": | |
values = resp['prefixes'] | |
identifier = "ip_prefix" | |
else: | |
values = resp['ipv6_prefixes'] | |
identifier = "ipv6_prefix" | |
prefixes = { | |
'identifier': identifier, | |
'values' : values, | |
} | |
return prefixes | |
def getValues(self) -> dict: | |
values = {} | |
prefixes = self.getPrefixes() | |
prefix_id = prefixes['identifier'] | |
ip_prefixes = [prefix[prefix_id] for prefix in prefixes['values']] | |
regions = [prefix['region'] for prefix in prefixes['values'] if prefix['region'] == self.region] | |
services = [prefix['service'] for prefix in prefixes['values']] | |
network_border_groups = [prefix['network_border_group'] for prefix in prefixes['values']] | |
values = { | |
'ip_prefixes' : ip_prefixes, | |
'regions' : regions, | |
'services' : services, | |
'network_border_groups' : network_border_groups, | |
} | |
return values | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Get prefix list IPs from particular AWS region.') | |
parser.add_argument("--region", type=str) | |
parser.add_argument("--type", type=str, default="ipv4") | |
args = parser.parse_args() | |
iprange = IpRanges(region=args.region, type=args.type) | |
print(iprange.getValues()['ip_prefixes']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[source]] | |
url = "https://pypi.org/simple" | |
verify_ssl = true | |
name = "pypi" | |
[packages] | |
urllib3 = "*" | |
[dev-packages] | |
[requires] | |
python_version = "3.9" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_meta": { | |
"hash": { | |
"sha256": "7776f372e3411c742b3e8a4209e67f0832f9e249d4c86ce28ece146afaef68d1" | |
}, | |
"pipfile-spec": 6, | |
"requires": { | |
"python_version": "3.9" | |
}, | |
"sources": [ | |
{ | |
"name": "pypi", | |
"url": "https://pypi.org/simple", | |
"verify_ssl": true | |
} | |
] | |
}, | |
"default": { | |
"urllib3": { | |
"hashes": [ | |
"sha256:4987c65554f7a2dbf30c18fd48778ef124af6fab771a377103da0585e2336ece", | |
"sha256:c4fdf4019605b6e5423637e01bc9fe4daef873709a7973e195ceba0a62bbc844" | |
], | |
"index": "pypi", | |
"version": "==1.26.7" | |
} | |
}, | |
"develop": {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment