Created
June 22, 2015 22:38
-
-
Save dgulinobw/31f93c961c641f0daf0e to your computer and use it in GitHub Desktop.
Takes stdin, highlights the IPs that are in a AWS IP range
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 | |
import sys | |
from blessings import Terminal | |
import re | |
import string | |
t = Terminal() | |
r = requests.get("https://ip-ranges.amazonaws.com/ip-ranges.json") | |
json = r.json() | |
prefixes = json["prefixes"] | |
def is_aws_ip(IP): | |
a = ipaddr.IPAddress(IP) | |
for prefix in prefixes: | |
range = prefix["ip_prefix"] | |
n = ipaddr.IPNetwork(range) | |
if n.Contains(a): | |
return True | |
return False | |
for line in sys.stdin: | |
ips = re.findall( r'[0-9]+(?:\.[0-9]+){3}', line ) | |
for ip in ips: | |
if is_aws_ip(ip): | |
line = string.replace( line, ip , "{t.bold_white_on_black}" + ip + "{t.normal}" ) | |
print(line.format(t=t), end='') | |
else: | |
print(line, end='') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment