def get_asn_for_ip(ip_addr): try: reader = Reader( os.path.join(GEOLOCATION_DATABASE_PATH, GEOLOCATION_DATABASE_ASN)) return reader.asn(ip_addr) except Exception as e: logger.warn("Failed retrieving asn info for %s: %s" % (ip_addr, str(e))) return None
def fetch_asn(ip_address): # Check if INCLUDE_ASN is True before proceeding if not config.INCLUDE_ASN: return None # Load GeoLite2 ASN database geoasn = Reader(path.join(config.MMDB_PATH, "GeoLite2-ASN.mmdb")) # Try to fetch data and build response, otherwise return empty try: data = geoasn.asn(ip_address) return { "name": data.autonomous_system_organization, "id": data.autonomous_system_number } except AddressNotFoundError: return {} finally: geoasn.close()
else: result[key] = current return result for message in consumer: # message value and key are raw bytes -- decode if necessary! # e.g., for unicode: `message.value.decode('utf-8')` # print ("%s:%d:%d: key=%s value=%s" % (message.topic, message.partition, # message.offset, message.key, # message.value)) data = json.loads(message.value) try: res = mmdb.city(data['dest_ip']) asn = asndb.asn(data['dest_ip']) data['dest_latitude'] = res.location.latitude data['dest_longitude'] = res.location.longitude data['dest_country_name'] = res.country.names['en'] data['dest_asn'] = asn.autonomous_system_number data['dest_autonomous_system'] = asn.autonomous_system_organization except: data['dest_latitude'] = 0.0 data['dest_longitude'] = 0.0 data['dest_country_name'] = 'unknown' data['dest_asn'] = 'unknown' data['dest_autonomous_system'] = 'unknown' try: res = mmdb.city(data['src_ip']) asn = asndb.asn(data['src_ip']) data['src_latitude'] = res.location.latitude