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 --- asn.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'asn.py') 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) -- cgit v1.2.3-54-g00ecf