Skip to content

Commit 3d2cb9d

Browse files
committed
Fix #719: convert macaddr in all sorts of forms to std format
Signed-off-by: Dinesh Dutt <[email protected]>
1 parent d1fd5b2 commit 3d2cb9d

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

suzieq/shared/utils.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,26 @@ def convert_macaddr_format_to_colon(macaddr: str) -> str:
460460
461461
"""
462462
if isinstance(macaddr, str):
463-
if re.match(r'[0-9a-zA-Z]{4}.[0-9a-zA-Z]{4}.[0-9a-zA-Z]{4}', macaddr):
463+
macaddr = macaddr.lower()
464+
if re.match(r'[0-9a-f]{4}\.[0-9a-f]{4}\.[0-9a-f]{4}', macaddr):
464465
return (':'.join([f'{x[:2]}:{x[2:]}'
465-
for x in macaddr.split('.')])).lower()
466-
else:
467-
return macaddr.lower()
466+
for x in macaddr.split('.')]))
467+
if re.match(r'[0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2}-'
468+
r'[0-9a-f]{2}-[0-9a-f]{2}-[0-9a-f]{2}',
469+
macaddr):
470+
return macaddr.replace('-', ':')
471+
if re.match(r'[0-9a-f]{4}:[0-9a-f]{4}:[0-9a-f]{4}', macaddr):
472+
return (':'.join([f'{x[:2]}:{x[2:]}'
473+
for x in macaddr.split(':')]))
474+
if re.match(r'[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}', macaddr):
475+
return (':'.join([f'{x[:2]}:{x[2:]}'
476+
for x in macaddr.split('-')]))
477+
if ':' not in macaddr and re.match(r'[0-9a-f]{12}', macaddr):
478+
newmac = ''
479+
for i in range(0, 12, 2):
480+
newmac += f'{macaddr[i:i+2]}:'
481+
newmac = newmac[:-1] # remove the trailing ':'
482+
return newmac
468483

469484
return '00:00:00:00:00:00'
470485

tests/unit/test_mac_convert.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from suzieq.shared.utils import convert_macaddr_format_to_colon
2+
3+
4+
def test_mac_convert():
5+
'''Test all formats of MAC formats being converted to std format'''
6+
result = '50:9a:4c:36:a1:df'
7+
nullmac = '00:00:00:00:00:00'
8+
9+
assert convert_macaddr_format_to_colon('509A:4C36:A1DF') == result, \
10+
'failed to convert 509A:4C36:A1DF'
11+
12+
assert convert_macaddr_format_to_colon('509A.4C36.A1DF') == result, \
13+
'failed to convert 509A.4C36.A1DF'
14+
15+
assert convert_macaddr_format_to_colon('509A-4C36-A1DF') == result, \
16+
'failed to convert 509A.4C36.A1DF'
17+
18+
assert convert_macaddr_format_to_colon('509A4C36A1DF') == result, \
19+
'failed to convert 509A4C36A1DF'
20+
21+
assert convert_macaddr_format_to_colon('50-9A-4C-36-A1-DF') == result, \
22+
'failed to convert 50-9A-4C-36-A1-DF'
23+
24+
assert convert_macaddr_format_to_colon('50-9Z-4C-36-A1-DF') == nullmac, \
25+
'Incorrect conversion of 50-9Z-4C-36-A1-DF'

0 commit comments

Comments
 (0)