Last active
October 24, 2021 04:40
-
-
Save franzramadhan/6bbb76632e0ab2912cd1bc8d352de02e to your computer and use it in GitHub Desktop.
Revisions
-
franzramadhan revised this gist
Oct 24, 2021 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1 +0,0 @@ -
franzramadhan revised this gist
Oct 24, 2021 . No changes.There are no files selected for viewing
-
franzramadhan revised this gist
Oct 24, 2021 . No changes.There are no files selected for viewing
-
franzramadhan revised this gist
Oct 24, 2021 . 3 changed files with 42 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ 3.9.7 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ [[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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ { "_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": {} } -
franzramadhan created this gist
Oct 24, 2021 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,97 @@ # 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'])