Check Which Region and Service an AWS IP is used for



Published: 2022-04-02 10:03:39 +0000
Categories: Misc, Python3,

Language

Python3

Description

Amazon Web Services (AWS) are vast, and use a significant range of IP blocks

Sometimes we want to try and ascertain where an AWS IP is used. Amazon publish details of their ranges and how they're used

This snippet provides a simple Python3 script to fetch AWS's listing and print details about an IP's usage

Snippet

#!/usr/bin/env python3
#
# Get details of where an AWS IP is used
#
# usage: search.py [ip]

import ipaddress
import requests
import sys

IP = ipaddress.ip_address(sys.argv[1])
r = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json')
j = r.json()

# Iterate over ranges
for range in j['prefixes']:
    if IP in ipaddress.ip_network(range['ip_prefix']):
        print(range)

Usage Example

./locate_aws_ip.py 143.204.170.78
# {'ip_prefix': '143.204.0.0/16', 'region': 'GLOBAL', 'service': 'AMAZON', 'network_border_group': 'GLOBAL'}
# {'ip_prefix': '143.204.0.0/16', 'region': 'GLOBAL', 'service': 'CLOUDFRONT', 'network_border_group': 'GLOBAL'}

License

BSD-3-Clause

Keywords

aws, python, ip, lookup, identify,

Latest Posts


Copyright © 2022 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk, http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion and http://snippets.bentasker.i2p
hit counter