Skip to content

Instantly share code, notes, and snippets.

@cedricbonhomme
Last active January 4, 2016 05:29
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 cedricbonhomme/8575427 to your computer and use it in GitHub Desktop.
Save cedricbonhomme/8575427 to your computer and use it in GitHub Desktop.
Get informations about IP banned by fail2ban.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import json
import requests
# https://github.com/CIRCL/bgpranking-redis-api/tree/master/example/api_web/client
from bgpranking_web import api
with open("/var/log/fail2ban.log", "r") as f:
log = f.read()
ip_candidates = re.findall(r"Ban (\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)", log)
#ip_candidates = ["221.8.18.15"]
for ip in ip_candidates:
result = requests.get("http://ipinfo.io/" + ip + "/json/")
json_result = json.loads(result.text)
asn_number = json_result["org"].split(" ")[0].replace("AS", "")
asn = api.asn_description(asn_number)
print("IP: " + ip)
print("Loc: %s / %s / %s" % (json_result["city"], json_result["region"], json_result["loc"]))
print("ASN: " + asn_number)
if None != asn:
print(asn["asn_description"])
print(asn["sources"])
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment