Skip to content

Instantly share code, notes, and snippets.

@dgulinobw
Last active August 29, 2015 14:23
Show Gist options
  • Save dgulinobw/bcb32dead32067f0cbba to your computer and use it in GitHub Desktop.
Save dgulinobw/bcb32dead32067f0cbba to your computer and use it in GitHub Desktop.
Determine if IP is an AWS IP, and what service it is serving.
#!/usr/bin/env python
from __future__ import print_function
import requests
import json
import ipaddr #py2
#import ipaddress #py3
import sys
import pprint
if len(sys.argv) == 1:
print('Usage: \n is_aws_ip.py IP')
exit(1)
IP = sys.argv[1]
a = ipaddr.IPAddress(IP)
#https://aws.amazon.com/blogs/aws/aws-ip-ranges-json/
r = requests.get("https://ip-ranges.amazonaws.com/ip-ranges.json")
json = r.json()
prefixes = json["prefixes"]
for prefix in prefixes:
range = prefix["ip_prefix"]
n = ipaddr.IPNetwork(range)
if n.Contains(a):
pprint.pprint(prefix)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment