aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan <me@jordan.im>2022-03-26 09:53:27 -0700
committerJordan <me@jordan.im>2022-03-26 09:53:27 -0700
commit355852b5f5421e428e6d08356a6d8bbc02d80af5 (patch)
treebd8961264fbaa304ac44d3496571a3f469bed836
parent35523e3b29e68e70fd6107b4637aa1443014a6d2 (diff)
downloadasn-355852b5f5421e428e6d08356a6d8bbc02d80af5.tar.gz
asn-355852b5f5421e428e6d08356a6d8bbc02d80af5.zip
asn, readme: format w/ ascii tables
-rw-r--r--README12
-rwxr-xr-xasn.py30
2 files changed, 31 insertions, 11 deletions
diff --git a/README b/README
index 85193d2..160d1c3 100644
--- a/README
+++ b/README
@@ -25,8 +25,14 @@ optional: forward traffic destined for port 43 (WHOIS default) back to 4343
USAGE
$ whois -h whois.jordan.im 1.1.1.1
- AS13335 AU CLOUDFLARENET 1.1.1.0/24
+
+ AS Number | Country | AS Name | Announcement
+ ----------+---------+---------------+-------------
+ 13335 | AU | CLOUDFLARENET | 1.1.1.0/24
$ whois -h whois.jordan.im jordan.im
- AS8943 GB Jump Networks Ltd 185.73.44.0/22
- AS8943 GB Jump Networks Ltd 2001:ba8::/32
+
+ AS Number | Country | AS Name | Announcement
+ ----------+---------+-------------------+---------------
+ 8943 | GB | Jump Networks Ltd | 185.73.44.0/22
+ 8943 | GB | Jump Networks Ltd | 2001:ba8::/32
diff --git a/asn.py b/asn.py
index 9d94cb2..a968280 100755
--- a/asn.py
+++ b/asn.py
@@ -39,10 +39,10 @@ class Listener:
log.info(f'{addr[0]} {recv_data}')
announcements = self._get_announcements(recv_data)
- announcements = self._pretty(announcements)
-
- if not announcements.strip():
+ if not announcements:
announcements = 'no valid hostname or IP discovered'
+ else:
+ announcements = self._pretty(announcements)
conn.sendall(bytes(announcements, 'utf-8'))
conn.shutdown(socket.SHUT_RDWR)
@@ -71,12 +71,26 @@ class Listener:
return announcements
- def _pretty(self, announcements):
- out = []
- for x in announcements:
- out.append(' '.join(('AS'+str(x[0]), x[1], x[2], x[3])))
+ def _pretty(self, announces):
+ announces = sorted(announces, key=lambda x: ipaddress.ip_network(
+ x[3]).version)
+
+ head = ('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)]
+
+ out = ''
+ header, data = announces[0], announces[1:]
+ out += ' | '.join(format(title,
+ "%ds" % width) for width, title in zip(w, header))
+ out += '\n' + '-+-'.join( '-' * width for width in w ) + '\n'
+
+ for row in data:
+ out += " | ".join(format(str(cdata),
+ "%ds" % width) for width, cdata in zip(w, row))
+ out += '\n'
- return '\n'.join(out) + '\n'
+ return out
def _resolve(self, hostname):
info = socket.getaddrinfo(hostname, 80, proto=socket.IPPROTO_TCP)