From 1697c95797fe37e388b23831910e94488100e698 Mon Sep 17 00:00:00 2001 From: Giggum <152240782+Giggum@users.noreply.github.com> Date: Mon, 27 May 2024 16:17:31 -0400 Subject: [PATCH] NAT: T6371: fix NAT op mode display of configured ports when comma ssperated list of ports/ranges exists --- src/op_mode/nat.py | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/op_mode/nat.py b/src/op_mode/nat.py index 4ab524fb7ac..53867aa7a3c 100755 --- a/src/op_mode/nat.py +++ b/src/op_mode/nat.py @@ -126,21 +126,33 @@ def _get_formatted_output_rules(data, direction, family): elif my_dict['field'] == 'daddr': daddr = f'{op}{my_dict["prefix"]["addr"]}/{my_dict["prefix"]["len"]}' elif my_dict['field'] == 'sport': - # Port range or single port - if jmespath.search('set[*].range', my_dict): - sport = my_dict['set'][0]['range'] - sport = '-'.join(map(str, sport)) - else: - sport = my_dict.get('set') - sport = ','.join(map(str, sport)) + # Get all configured sport ranges or single ports + for index, port in enumerate(my_dict['set']): + if 'range' in str(my_dict['set'][index]): + temp = my_dict['set'][index]['range'] + temp = '-'.join(map(str, temp)) + else: + temp = str(port) + if index == 0: + sport = str(temp) + else: + sport = ','.join([sport,temp]) + if my_dict['op'] == '!=': + sport = '!' + sport elif my_dict['field'] == 'dport': - # Port range or single port - if jmespath.search('set[*].range', my_dict): - dport = my_dict["set"][0]["range"] - dport = '-'.join(map(str, dport)) - else: - dport = my_dict.get('set') - dport = ','.join(map(str, dport)) + # Get all configured dport ranges or single ports + for index, port in enumerate(my_dict['set']): + if 'range' in str(my_dict['set'][index]): + temp = my_dict['set'][index]['range'] + temp = '-'.join(map(str, temp)) + else: + temp = str(port) + if index == 0: + dport = str(temp) + else: + dport = ','.join([dport,temp]) + if my_dict['op'] == '!=': + dport = '!' + dport else: field = jmespath.search('left.payload.field', match) if field == 'saddr':