Skip to content

Instantly share code, notes, and snippets.

@t94j0
Created June 18, 2024 17:50
Show Gist options
  • Save t94j0/d7a6f8f6b39cd55848299852b4911eab to your computer and use it in GitHub Desktop.
Save t94j0/d7a6f8f6b39cd55848299852b4911eab to your computer and use it in GitHub Desktop.
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