ARP table: IP addresses natural sort

This commit is contained in:
moi 2022-09-12 23:12:15 +02:00
parent cdc4189b1b
commit 5f4cd3306f

View File

@ -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