Created
June 18, 2024 17:50
-
-
Save t94j0/d7a6f8f6b39cd55848299852b4911eab to your computer and use it in GitHub Desktop.
This file contains 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
import ipaddress | |
import requests | |
from argparse import ArgumentParser | |
def load_iprange() -> list[str]: | |
data = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json').json() | |
return [data['ip_prefix'] for data in data['prefixes']] | |
def check_ip(ip: str) -> bool: | |
target = ipaddress.ip_address(ip) | |
ranges = load_iprange() | |
for r in ranges: | |
if target in ipaddress.ip_network(r): | |
return True | |
def main(): | |
parser = ArgumentParser() | |
parser.add_argument('ip', help='IP Address to check') | |
args = parser.parse_args() | |
if check_ip(args.ip): | |
print(f'{args.ip} is in AWS IP range') | |
else: | |
print(f'{args.ip} is not in AWS IP range') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment