From: Michael Tremer Date: Thu, 6 Feb 2020 15:28:03 +0000 (+0000) Subject: tools: Fetch organization names for ASes from WHOIS X-Git-Url: http://git.ipfire.org/?p=location%2Flocation-database.git;a=commitdiff_plain;h=8e49f39558bb4d946ac7896a239a0577308d4503 tools: Fetch organization names for ASes from WHOIS Signed-off-by: Michael Tremer --- diff --git a/tools/base.py b/tools/base.py index bdd7fbd..7ee3978 100644 --- a/tools/base.py +++ b/tools/base.py @@ -28,6 +28,7 @@ import os.path import re import sqlite3 import struct +import subprocess from . import downloader from . import util @@ -49,6 +50,8 @@ class RIR(object): name = None database_urls = [] + whois_server = None + def __init__(self): pass @@ -82,6 +85,36 @@ class RIR(object): # Write the database to disk p.export_database(directory) + def whois(self, query): + command = [ + "whois", query, + ] + + # Query a specific WHOIS server + if self.whois_server: + command += ["-h", self.whois_server] + + logging.info("Running command: %s" % " ".join(command)) + + try: + output = subprocess.check_output(command, stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: + logging.error("Could not run WHOIS query %s: %s" % (query, e.output)) + raise + + return output.decode(errors="ignore") + + def get_name_for_asn(self, asn): + result = self.whois("AS%s" % asn) + + for line in result.splitlines(): + key, delim, value = line.partition(":") + if not value: + continue + + if key in ("org-name", "OrgName"): + return value.strip() + class RIRParser(object): def __init__(self, rir): @@ -315,6 +348,10 @@ class RIRParser(object): f.write(FMT % ("asnum:", "AS%s" % asn)) + name = self.rir.get_name_for_asn(asn) + if name: + f.write(FMT % ("name:", name)) + if country: f.write(FMT % ("country:", country))