]> git.ipfire.org Git - location/location-database.git/commitdiff
tools: Fetch organization names for ASes from WHOIS
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 6 Feb 2020 15:28:03 +0000 (15:28 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 6 Feb 2020 15:28:03 +0000 (15:28 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tools/base.py

index bdd7fbd656aa400c1fa97dcefe401adaea8ec1ef..7ee3978f479c34c3819973f668cc4249010757ea 100644 (file)
@@ -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))