diff options
Diffstat (limited to 'asn.py')
-rwxr-xr-x | asn.py | 23 |
1 files changed, 12 insertions, 11 deletions
@@ -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) |