From 5f4cd3306f096b0e682abd7ce2e23ec2b1d084d6 Mon Sep 17 00:00:00 2001 From: moi Date: Mon, 12 Sep 2022 23:12:15 +0200 Subject: [PATCH] ARP table: IP addresses natural sort --- src/opnsense/scripts/interfaces/list_arp.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/opnsense/scripts/interfaces/list_arp.py b/src/opnsense/scripts/interfaces/list_arp.py index bc23fbff6..805b31e72 100755 --- a/src/opnsense/scripts/interfaces/list_arp.py +++ b/src/opnsense/scripts/interfaces/list_arp.py @@ -36,6 +36,14 @@ import netaddr sys.path.insert(0, "/usr/local/opnsense/site-python") import watchers.dhcpd +import re +def natural_sort(list, key=lambda s:s): + def get_alphanum_key_func(key): + convert = lambda text: int(text) if text.isdigit() else text + return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] + sort_key = get_alphanum_key_func(key) + list.sort(key=sort_key) + if __name__ == '__main__': @@ -66,6 +74,8 @@ if __name__ == '__main__': sp = subprocess.run(['/usr/sbin/arp', '-a'+arp_arg, '--libxo','json'], capture_output=True, text=True) libxo_out = ujson.loads(sp.stdout) arp_cache = libxo_out['arp']['arp-cache'] if 'arp' in libxo_out and 'arp-cache' in libxo_out['arp'] else [] + natural_sort(arp_cache, key=lambda s: s['ip-address']) + for src_record in arp_cache: if 'incomplete' in src_record and src_record['incomplete'] is True: continue