import re
import sqlite3
import struct
+import subprocess
from . import downloader
from . import util
name = None
database_urls = []
+ whois_server = None
+
def __init__(self):
pass
# 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):
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))