Skip to content

Instantly share code, notes, and snippets.

@ryanlovett
Last active March 11, 2016 18:04
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 ryanlovett/f86525ffa968bff0f303 to your computer and use it in GitHub Desktop.
Save ryanlovett/f86525ffa968bff0f303 to your computer and use it in GitHub Desktop.
Converts SNS AID data to a list of hosts.
#!/usr/bin/env python3
'''
Convert SNS AID data to a list for ipf.
'''
import os
import itertools
import json
from optparse import OptionParser
CACHE_FILE = '/var/cache/sns-aid.json'
# https://docs.python.org/3/library/itertools.html
def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
# unique_everseen('ABBCcAD', str.lower) --> A B C D
seen = set()
seen_add = seen.add
if key is None:
for element in itertools.filterfalse(seen.__contains__, iterable):
seen_add(element)
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
seen_add(k)
yield element
def aggressive_ips(data):
buf = open(CACHE_FILE).read()
data = json.loads(buf)
# We group to avoid duplicates
for ip in unique_everseen(map(lambda x: x['ip'], data['aggressive_ips'])):
print(ip)
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-f", dest="cache", default=CACHE_FILE,
help="sns aid cache file")
(options, args) = parser.parse_args()
aggressive_ips(CACHE_FILE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment