Skip to content

Instantly share code, notes, and snippets.

@reecestart
Created October 14, 2022 02:54
Show Gist options
  • Save reecestart/1c6cb7ece2bb59d055557435ccdc283c to your computer and use it in GitHub Desktop.
Save reecestart/1c6cb7ece2bb59d055557435ccdc283c to your computer and use it in GitHub Desktop.
Checks IP Range to see if it is in the AWS IP Ranges (and what it is assigned to)
#!/usr/bin/env python
import requests
import ipaddr
# Get the AWS IP address ranges https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html
ip_ranges = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json').json()['prefixes']
# Get the CIDR to check from from the user
cidrtocheck = input("Enter your CIDR to check... ")
range = ipaddr.IPNetwork(cidrtocheck) # Turn it into IP Network Class
# For each of the AWS IP Ranges...
for i in ip_ranges:
awsrange = ipaddr.IPNetwork(i['ip_prefix']) # Get the AWS IP Range
if range.overlaps(awsrange):
print(i) # Check if it overlaps and if so print it out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment