From 9f7cecdb131f7295e315bc9ea6c829025b773188 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sun, 24 Jul 2022 09:40:07 -0700 Subject: asn: return A/AAAA record(s) in reply --- README | 14 +++++++------- asn.py | 23 ++++++++++++----------- location-database | 2 +- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/README b/README index 160d1c3..1224343 100644 --- a/README +++ b/README @@ -26,13 +26,13 @@ USAGE $ whois -h whois.jordan.im 1.1.1.1 - AS Number | Country | AS Name | Announcement - ----------+---------+---------------+------------- - 13335 | AU | CLOUDFLARENET | 1.1.1.0/24 + IP Address | AS Number | Country | AS Name | Announcement + -----------+-----------+---------+---------------+------------- + 1.1.1.1 | 13335 | AU | CLOUDFLARENET | 1.1.1.0/24 $ whois -h whois.jordan.im jordan.im - AS Number | Country | AS Name | Announcement - ----------+---------+-------------------+--------------- - 8943 | GB | Jump Networks Ltd | 185.73.44.0/22 - 8943 | GB | Jump Networks Ltd | 2001:ba8::/32 + IP Address | AS Number | Country | AS Name | Announcement + ------------------+-----------+---------+-------------------+--------------- + 185.73.44.145 | 8943 | GB | Jump Networks Ltd | 185.73.44.0/22 + 2001:ba8:0:4031:: | 8943 | GB | Jump Networks Ltd | 2001:ba8::/32 diff --git a/asn.py b/asn.py index f79ee6c..0eb8c6c 100755 --- a/asn.py +++ b/asn.py @@ -68,19 +68,24 @@ class Listener: announcements = [] for host in hosts: if self._is_invalid(host): - return [] + continue n = self._get_netblock(host) - if n: - announcements.extend(self.db.query(n)) + if not n: + continue + + res = self.db.query(n) + if res: + res = list(res[0]) + res.insert(0, str(host)) + announcements.extend([res]) return announcements def _pretty(self, announces): - announces = sorted(announces, key=lambda x: ipaddress.ip_network( - x[3]).version) + announces = sorted(announces, key=lambda x: ipaddress.ip_network(x[4]).version) - head = ('AS Number', 'Country', 'AS Name', 'Announcement') + head = ('IP Address', 'AS Number', 'Country', 'AS Name', 'Announcement') announces.insert(0, head) w = [len(max(i, key=lambda x: len(str(x)))) for i in zip(*announces)] @@ -229,7 +234,6 @@ class DB: ''', (kv.get('aut-num'), kv.get('net'), kv.get('country'))) def query(self, net): - announcements = [] while True: rows = self.con.execute(''' SELECT net.aut_num, net.country, asn.name, net.net @@ -240,15 +244,12 @@ class DB: WHERE net.net = ? ''', (str(net), str(net))).fetchall() if len(rows) != 0: - announcements.extend(rows) - break + return rows if net.prefixlen > 0: net = net.supernet() else: break - return announcements - if __name__ == '__main__': desc = 'asn: map hosts to their corresponding ASN via WHOIS' parser = argparse.ArgumentParser(description=desc) diff --git a/location-database b/location-database index 80cd5c7..07aa120 160000 --- a/location-database +++ b/location-database @@ -1 +1 @@ -Subproject commit 80cd5c76ee0c04960da618c18f2feaad2c1f5f0f +Subproject commit 07aa1203299d4e97d27b1faec65baf3535fe8f0d -- cgit v1.2.3-54-g00ecf