Last active
August 29, 2015 14:23
-
-
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.
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
#!/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