From 093aff35e40ebce128dba7df6e98288d0be288c2 Mon Sep 17 00:00:00 2001 From: adrien gaultier Date: Wed, 9 Oct 2024 17:59:38 +0200 Subject: [PATCH] fix showmaps --- Justfile | 2 +- showmaps.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Justfile b/Justfile index c035e45..c3e9492 100644 --- a/Justfile +++ b/Justfile @@ -39,7 +39,7 @@ show_active_maps: map_ids=$(bpftool map show | grep "$MAPTYPE" | cut -f1 -d":" ); \ for map_id in $map_ids;do \ echo "$MAPTYPE($map_id)";\ - bpftool map dump id $map_id -j | jq "." | python3 showmaps.py 2>/dev/null || echo "\tempty";\ + bpftool map dump id $map_id -j | python3 showmaps.py 2>/dev/null || echo "\tempty";\ done ;\ done diff --git a/showmaps.py b/showmaps.py index c18123d..a9b7293 100644 --- a/showmaps.py +++ b/showmaps.py @@ -1,9 +1,16 @@ import sys,json; rules=json.loads(sys.stdin.read()) for rule in rules: - k = '.'.join([str(int(x,16)) for x in rule['key']]) - chunks =[rule['value'][idx: idx+2] for idx in range(0,len(rule['value']),2)] - ports = [int(f"{chunk[1]}{chunk[0]}".replace("0x",""),16) for chunk in chunks] + ip_version = "ipv4" if len(rule['key'])== 4 else "ipv6" + if ip_version=="ipv6": + chunks_k = [rule['key'][idx: idx+2] for idx in range(0,len(rule['key']),2)] + k = ':'.join( reversed([f"{chunk[1]}{chunk[0]}".replace("0x","") for chunk in chunks_k])) + elif ip_version=="ipv4": + k = '.'.join( reversed([str(int(k.replace("0x",""),16)) for k in rule['key']])) + else: + raise ValueError("wrong ip version") + chunks_v =[rule['value'][idx: idx+2] for idx in range(0,len(rule['value']),2)] + ports = [int(f"{chunk[1]}{chunk[0]}".replace("0x",""),16) for chunk in chunks_v] v = "[{}, ...]".format(', '.join([str(k) for k in ports if k!=0])) if not all(map(lambda x: x==0,ports)) else '*' print(f"\t{k} : {v}")