Feb-20-2018, 12:12 PM
output="""dishash all mac address | bport | vlan | flow agg |static|aging|proccess|deny_sa ========================================================================== e8 37 7a 9d 1a f1 | 1616 | 3612 | 44200059 | 0 | 1 | 0 | 0 54 b8 a 49 97 d5 | 1696 | 3612 | 442000a5 | 0 | 1 | 0 | 0 b0 b2 dc 5 d 87 | 1730 | 3612 | 442000c7 | 0 | 1 | 0 | 0 0 16 fa d0 be a1 | 300 | 4094 | 4420002c | 1 | 0 | 0 | 0 28 28 5d ed b4 41 | 1640 | 3612 | 4420006f | 0 | 0 | 0 | 0 0 17 7c 76 99 4d | 1622 | 3612 | 4420005d | 0 | 1 | 0 | 0 28 28 5d 94 78 da | 1704 | 3612 | 442000ad | 0 | 1 | 0 | 0 48 ee c 95 8 ee | 1630 | 3612 | 44200065 | 0 | 1 | 0 | 0 48 ee c bf 49 65 | 1628 | 3612 | 44200063 | 0 | 1 | 0 | 0 0 17 7c 80 7c 4a | 1732 | 3612 | 442000c9 | 0 | 1 | 0 | 0 e8 cc 18 62 4b d3 | 1736 | 3612 | 442000cd | 0 | 1 | 0 | 0 0 17 7c 61 c2 c2 | 1596 | 3612 | 44200047 | 0 | 1 | 0 | 0 0 25 5e 3f e7 18 | 1716 | 3612 | 442000b9 | 0 | 1 | 0 | 0 cc 5d 4e 26 e8 e2 | 1706 | 3612 | 442000af | 0 | 1 | 0 | 0 c d2 b5 5f 62 6e | 1632 | 3612 | 44200067 | 0 | 1 | 0 | 0 28 28 5d f4 f dc | 1680 | 3612 | 44200095 | 0 | 1 | 0 | 0 6c 72 20 ee a8 66 | 1594 | 3612 | 44200045 | 0 | 1 | 0 | 0 e8 37 7a 9d e 6b | 1734 | 3612 | 442000cb | 0 | 1 | 0 | 0 cc 5d 4e 26 e2 30 | 1698 | 3612 | 442000a7 | 0 | 1 | 0 | 0 c d2 b5 63 51 87 | 1710 | 3612 | 442000b3 | 0 | 1 | 0 | 0 Total number of MAC addresses: 20 MCR64A >""" def mac2norm(mac): mac = mac.split(' ') return '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac).upper() header = ('mac address', 'bport', 'vlan') print('{:^17} | {:^5} | {:^4}'.format(*header)) print('='*33) for line in output.split('\n')[4:]: line = line.split('|') if len(line) == 8: mac, bport, vlan = map(lambda x: x.strip(), line[:3]) mac_address = mac2norm(mac) print('{:^17} | {: >5} | {: >4}'.format(mac_address, bport, vlan))
Output: mac address | bport | vlan
=================================
E8:37:7A:9D:1A | 1616 | 3612
54:B8:00:0A:49 | 1696 | 3612
B0:B2:DC:00:05 | 1730 | 3612
00:16:FA:D0:BE | 300 | 4094
28:28:5D:ED:B4 | 1640 | 3612
00:17:7C:76:99 | 1622 | 3612
28:28:5D:94:78 | 1704 | 3612
48:EE:00:0C:95 | 1630 | 3612
48:EE:00:0C:BF | 1628 | 3612
00:17:7C:80:7C | 1732 | 3612
E8:CC:18:62:4B | 1736 | 3612
00:17:7C:61:C2 | 1596 | 3612
00:25:5E:3F:E7 | 1716 | 3612
CC:5D:4E:26:E8 | 1706 | 3612
0C:D2:B5:5F:62 | 1632 | 3612
28:28:5D:F4:00 | 1680 | 3612
6C:72:20:EE:A8 | 1594 | 3612
E8:37:7A:9D:00 | 1734 | 3612
CC:5D:4E:26:E2 | 1698 | 3612
0C:D2:B5:63:51 | 1710 | 3612
>>>
for line in output.split('\n')[4:]: line = line.split('|') if len(line) == 8: mac, bport, vlan = map(lambda x: x.strip(), line[:3]) mac_address = mac2norm(mac) print('{:^17} | {: >5} | {: >4}'.format(mac_address, bport, vlan))could be also
for line in output.split('\n')[4:-4]: line = line.split('|') mac, bport, vlan = map(lambda x: x.strip(), line[:3]) mac_address = mac2norm(mac) print('{:^17} | {: >5} | {: >4}'.format(mac_address, bport, vlan))