Skip to content

Instantly share code, notes, and snippets.

@dgulinobw
Created June 22, 2015 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgulinobw/31f93c961c641f0daf0e to your computer and use it in GitHub Desktop.
Save dgulinobw/31f93c961c641f0daf0e to your computer and use it in GitHub Desktop.
Takes stdin, highlights the IPs that are in a AWS IP range
#!/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