Python Forum

Full Version: Help required to print MAC addresses
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6
Did you see this: https://python-forum.io/Thread-Help-requ...2#pid40032

Also, did you try this: https://python-forum.io/Thread-Help-requ...8#pid39938

I don't get it, why I can't reproduce these errors Dodgy
for line in output.decode('utf-8').split('\n')[2:]:
    print (line)
    data = [field.strip() for field in line.split('|')[:3]]
    print (data)
    mac = [elm for elm in data[0].split() if elm]
    mac_addr = '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac)
    bport, vlan = data[1:]
    print("{!s:^23}\t{!s:^5}\t{!s:^5}".format(mac, bport, vlan))
Yes Wavic, already tried.below is the output, tuple is out of range

Output:
MCR64A > mac address | bport | vlan | flow agg |static|aging|proccess|deny_sa [u'mac address', u'bport', u'vlan'] Traceback (most recent call last): File "ecicollector-1.py", line 40, in <module> mac_addr = '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac) IndexError: tuple index out of range
Well, print 'mac' to see what it holds.
Hi Wavic,
mac:[u'mac', u'address']

Output:
line: mac address | bport | vlan | flow agg |static|aging|proccess|deny_sa data :[u'mac address', u'bport', u'vlan'] mac:[u'mac', u'address'] Traceback (most recent call last): File "ecicollector-1.py", line 41, in <module> mac_addr = '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac) IndexError: tuple index out of range
Put a print above the error line to see what 'mac' variable contains.
code

for line in output.decode('utf-8').split('\n')[2:]:
    print ('line: {}'.format(line)) # Printing - Line
    data = [field.strip() for field in line.split('|')[:3]]
    print ('data :{}'.format(data)) #Printing Data
    mac = [elm for elm in data[0].split() if elm]
    print ('mac:{}'.format(mac)) #Printing MAC
    mac_addr = '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac) # Tuple Out of index
    bport, vlan = data[1:]
    print("{:^23}\t{:^5}\t{:^5}".format(mac, bport, vlan))
Error:
line: mac address | bport | vlan | flow agg |static|aging|proccess|deny_sa data :[u'mac address', u'bport', u'vlan'] mac:[u'mac', u'address'] Traceback (most recent call last): File "ecicollector-1.py", line 41, in <module> mac_addr = '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac) IndexError: tuple index out of range
Is that
Output:
dishash all
part of the 'output'? If it is, change the slicing digit to 4 in the loop expression:

for line in output.decode('utf-8').split('\n')[4:]:
yes,
Output:
dishash all
is part of output.

Changes in loop

for line in output.decode('utf-8').split('\n')[4:]:
    print ('line: {}'.format(line))
    data = [field.strip() for field in line.split('|')[:3]]
    print ('data :{}'.format(data))
    mac = [elm for elm in data[0].split() if elm]
    print ('mac:{}'.format(mac))
    mac_addr = '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac)
    bport, vlan = data[1:]
    print("{:^23}\t{:^5}\t{:^5}".format(mac, bport, vlan))
Current output
Output:
line: e8 37 7a 9d 1a f1 | 1616 | 3612 | 44200059 | 0 | 1 | 0 | 0 data :[u'e8 37 7a 9d 1a f1', u'1616', u'3612'] mac:[u'e8', u'37', u'7a', u'9d', u'1a', u'f1'] [u'e8', u'37', u'7a', u'9d', u'1a', u'f1'] 1616 3612 line: 54 b8 a 49 97 d5 | 1696 | 3612 | 442000a5 | 0 | 1 | 0 | 0 data :[u'54 b8 a 49 97 d5', u'1696', u'3612'] mac:[u'54', u'b8', u'a', u'49', u'97', u'd5'] [u'54', u'b8', u'a', u'49', u'97', u'd5'] 1696 3612 line: b0 b2 dc 5 d 87 | 1730 | 3612 | 442000c7 | 0 | 1 | 0 | 0 data :[u'b0 b2 dc 5 d 87', u'1730', u'3612'] mac:[u'b0', u'b2', u'dc', u'5', u'd', u'87'] [u'b0', u'b2', u'dc', u'5', u'd', u'87'] 1730 3612 line: 0 16 fa d0 be a1 | 300 | 4094 | 4420002c | 1 | 0 | 0 | 0 data :[u'0 16 fa d0 be a1', u'300', u'4094'] mac:[u'0', u'16', u'fa', u'd0', u'be', u'a1'] [u'0', u'16', u'fa', u'd0', u'be', u'a1'] 300 4094 line: 28 28 5d ed b4 41 | 1640 | 3612 | 4420006f | 0 | 1 | 0 | 0 data :[u'28 28 5d ed b4 41', u'1640', u'3612'] mac:[u'28', u'28', u'5d', u'ed', u'b4', u'41'] [u'28', u'28', u'5d', u'ed', u'b4', u'41'] 1640 3612 line: 0 17 7c 76 99 4d | 1622 | 3612 | 4420005d | 0 | 1 | 0 | 0 data :[u'0 17 7c 76 99 4d', u'1622', u'3612'] mac:[u'0', u'17', u'7c', u'76', u'99', u'4d'] [u'0', u'17', u'7c', u'76', u'99', u'4d'] 1622 3612 line: 28 28 5d 94 78 da | 1704 | 3612 | 442000ad | 0 | 1 | 0 | 0 data :[u'28 28 5d 94 78 da', u'1704', u'3612'] mac:[u'28', u'28', u'5d', u'94', u'78', u'da'] [u'28', u'28', u'5d', u'94', u'78', u'da'] 1704 3612 line: 48 ee c 95 8 ee | 1630 | 3612 | 44200065 | 0 | 1 | 0 | 0 data :[u'48 ee c 95 8 ee', u'1630', u'3612'] mac:[u'48', u'ee', u'c', u'95', u'8', u'ee'] [u'48', u'ee', u'c', u'95', u'8', u'ee'] 1630 3612 line: 48 ee c bf 49 65 | 1628 | 3612 | 44200063 | 0 | 1 | 0 | 0 data :[u'48 ee c bf 49 65', u'1628', u'3612'] mac:[u'48', u'ee', u'c', u'bf', u'49', u'65'] [u'48', u'ee', u'c', u'bf', u'49', u'65'] 1628 3612 line: 0 17 7c 80 7c 4a | 1732 | 3612 | 442000c9 | 0 | 1 | 0 | 0 data :[u'0 17 7c 80 7c 4a', u'1732', u'3612'] mac:[u'0', u'17', u'7c', u'80', u'7c', u'4a'] [u'0', u'17', u'7c', u'80', u'7c', u'4a'] 1732 3612 line: e8 cc 18 62 4b d3 | 1736 | 3612 | 442000cd | 0 | 1 | 0 | 0 data :[u'e8 cc 18 62 4b d3', u'1736', u'3612'] mac:[u'e8', u'cc', u'18', u'62', u'4b', u'd3'] [u'e8', u'cc', u'18', u'62', u'4b', u'd3'] 1736 3612 line: 0 25 5e 3f e7 18 | 1716 | 3612 | 442000b9 | 0 | 1 | 0 | 0 data :[u'0 25 5e 3f e7 18', u'1716', u'3612'] mac:[u'0', u'25', u'5e', u'3f', u'e7', u'18'] [u'0', u'25', u'5e', u'3f', u'e7', u'18'] 1716 3612 line: cc 5d 4e 26 e8 e2 | 1706 | 3612 | 442000af | 0 | 1 | 0 | 0 data :[u'cc 5d 4e 26 e8 e2', u'1706', u'3612'] mac:[u'cc', u'5d', u'4e', u'26', u'e8', u'e2'] [u'cc', u'5d', u'4e', u'26', u'e8', u'e2'] 1706 3612 line: c d2 b5 5f 62 6e | 1632 | 3612 | 44200067 | 0 | 1 | 0 | 0 data :[u'c d2 b5 5f 62 6e', u'1632', u'3612'] mac:[u'c', u'd2', u'b5', u'5f', u'62', u'6e'] [u'c', u'd2', u'b5', u'5f', u'62', u'6e'] 1632 3612 line: 28 28 5d f4 f dc | 1680 | 3612 | 44200095 | 0 | 1 | 0 | 0 data :[u'28 28 5d f4 f dc', u'1680', u'3612'] mac:[u'28', u'28', u'5d', u'f4', u'f', u'dc'] [u'28', u'28', u'5d', u'f4', u'f', u'dc'] 1680 3612 line: cc 5d 4e 26 e2 30 | 1698 | 3612 | 442000a7 | 0 | 1 | 0 | 0 data :[u'cc 5d 4e 26 e2 30', u'1698', u'3612'] mac:[u'cc', u'5d', u'4e', u'26', u'e2', u'30'] [u'cc', u'5d', u'4e', u'26', u'e2', u'30'] 1698 3612 line: 0 26 15 11 7a fa | 1674 | 3612 | 4420008f | 0 | 1 | 0 | 0 data :[u'0 26 15 11 7a fa', u'1674', u'3612'] mac:[u'0', u'26', u'15', u'11', u'7a', u'fa'] [u'0', u'26', u'15', u'11', u'7a', u'fa'] 1674 3612 line: c d2 b5 63 51 87 | 1710 | 3612 | 442000b3 | 0 | 1 | 0 | 0 data :[u'c d2 b5 63 51 87', u'1710', u'3612'] mac:[u'c', u'd2', u'b5', u'63', u'51', u'87'] [u'c', u'd2', u'b5', u'63', u'51', u'87'] 1710 3612 line: data :[u''] mac:[]

Error:
Traceback (most recent call last): File "ecicollector-1.py", line 41, in <module> mac_addr = '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac) IndexError: tuple index out of range
Is the

Output:
Total number of MAC addresses: 19
part of the 'output'. I am always keep getting just

Output:
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 | 1 | 0 | 0 0 17 7c 76 99 4d | 1622 | 3612 | 4420005d | 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 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 0 26 15 11 7a fa | 1674 | 3612 | 4420008f | 0 | 1 | 0 | 0 c d2 b5 63 51 87 | 1710 | 3612 | 442000b3 | 0 | 1 | 0 | 0
when I parse it.

You are completely right in post 10 ( https://python-forum.io/Thread-Help-requ...4#pid39914 ). I just didn't see the whole post.

Change the for expression to exclude the last two lines. The empty line and the summarize.
for line in output.decode('utf-8').split('\n')[4:-2]:
Hi Wavic,


Output:
line: 54 b8 a 49 97 d5 | 1696 | 3612 | 442000a5 | 0 | 1 | 0 | 0 data :[u'54 b8 a 49 97 d5', u'1696', u'3612'] mac:[u'54', u'b8', u'a', u'49', u'97', u'd5'] [u'54', u'b8', u'a', u'49', u'97', u'd5'] 1696 3612 line: b0 b2 dc 5 d 87 | 1730 | 3612 | 442000c7 | 0 | 1 | 0 | 0 data :[u'b0 b2 dc 5 d 87', u'1730', u'3612'] mac:[u'b0', u'b2', u'dc', u'5', u'd', u'87'] [u'b0', u'b2', u'dc', u'5', u'd', u'87'] 1730 3612 line: 0 16 fa d0 be a1 | 300 | 4094 | 4420002c | 1 | 0 | 0 | 0 data :[u'0 16 fa d0 be a1', u'300', u'4094'] mac:[u'0', u'16', u'fa', u'd0', u'be', u'a1'] [u'0', u'16', u'fa', u'd0', u'be', u'a1'] 300 4094 line: 28 28 5d ed b4 41 | 1640 | 3612 | 4420006f | 0 | 1 | 0 | 0 data :[u'28 28 5d ed b4 41', u'1640', u'3612'] mac:[u'28', u'28', u'5d', u'ed', u'b4', u'41'] [u'28', u'28', u'5d', u'ed', u'b4', u'41'] 1640 3612 line: 0 17 7c 76 99 4d | 1622 | 3612 | 4420005d | 0 | 1 | 0 | 0 data :[u'0 17 7c 76 99 4d', u'1622', u'3612'] mac:[u'0', u'17', u'7c', u'76', u'99', u'4d'] [u'0', u'17', u'7c', u'76', u'99', u'4d'] 1622 3612 line: 28 28 5d 94 78 da | 1704 | 3612 | 442000ad | 0 | 1 | 0 | 0 data :[u'28 28 5d 94 78 da', u'1704', u'3612'] mac:[u'28', u'28', u'5d', u'94', u'78', u'da'] [u'28', u'28', u'5d', u'94', u'78', u'da'] 1704 3612 line: 48 ee c 95 8 ee | 1630 | 3612 | 44200065 | 0 | 1 | 0 | 0 data :[u'48 ee c 95 8 ee', u'1630', u'3612'] mac:[u'48', u'ee', u'c', u'95', u'8', u'ee'] [u'48', u'ee', u'c', u'95', u'8', u'ee'] 1630 3612 line: 48 ee c bf 49 65 | 1628 | 3612 | 44200063 | 0 | 1 | 0 | 0 data :[u'48 ee c bf 49 65', u'1628', u'3612'] mac:[u'48', u'ee', u'c', u'bf', u'49', u'65'] [u'48', u'ee', u'c', u'bf', u'49', u'65'] 1628 3612 line: 0 17 7c 80 7c 4a | 1732 | 3612 | 442000c9 | 0 | 1 | 0 | 0 data :[u'0 17 7c 80 7c 4a', u'1732', u'3612'] mac:[u'0', u'17', u'7c', u'80', u'7c', u'4a'] [u'0', u'17', u'7c', u'80', u'7c', u'4a'] 1732 3612 line: e8 cc 18 62 4b d3 | 1736 | 3612 | 442000cd | 0 | 1 | 0 | 0 data :[u'e8 cc 18 62 4b d3', u'1736', u'3612'] mac:[u'e8', u'cc', u'18', u'62', u'4b', u'd3'] [u'e8', u'cc', u'18', u'62', u'4b', u'd3'] 1736 3612 line: 0 17 7c 61 c2 c2 | 1596 | 3612 | 44200047 | 0 | 1 | 0 | 0 data :[u'0 17 7c 61 c2 c2', u'1596', u'3612'] mac:[u'0', u'17', u'7c', u'61', u'c2', u'c2'] [u'0', u'17', u'7c', u'61', u'c2', u'c2'] 1596 3612 line: 0 25 5e 3f e7 18 | 1716 | 3612 | 442000b9 | 0 | 1 | 0 | 0 data :[u'0 25 5e 3f e7 18', u'1716', u'3612'] mac:[u'0', u'25', u'5e', u'3f', u'e7', u'18'] [u'0', u'25', u'5e', u'3f', u'e7', u'18'] 1716 3612 line: cc 5d 4e 26 e8 e2 | 1706 | 3612 | 442000af | 0 | 1 | 0 | 0 data :[u'cc 5d 4e 26 e8 e2', u'1706', u'3612'] mac:[u'cc', u'5d', u'4e', u'26', u'e8', u'e2'] [u'cc', u'5d', u'4e', u'26', u'e8', u'e2'] 1706 3612 line: c d2 b5 5f 62 6e | 1632 | 3612 | 44200067 | 0 | 1 | 0 | 0 data :[u'c d2 b5 5f 62 6e', u'1632', u'3612'] mac:[u'c', u'd2', u'b5', u'5f', u'62', u'6e'] [u'c', u'd2', u'b5', u'5f', u'62', u'6e'] 1632 3612 line: 28 28 5d f4 f dc | 1680 | 3612 | 44200095 | 0 | 1 | 0 | 0 data :[u'28 28 5d f4 f dc', u'1680', u'3612'] mac:[u'28', u'28', u'5d', u'f4', u'f', u'dc'] [u'28', u'28', u'5d', u'f4', u'f', u'dc'] 1680 3612 line: 6c 72 20 ee a8 66 | 1594 | 3612 | 44200045 | 0 | 1 | 0 | 0 data :[u'6c 72 20 ee a8 66', u'1594', u'3612'] mac:[u'6c', u'72', u'20', u'ee', u'a8', u'66'] [u'6c', u'72', u'20', u'ee', u'a8', u'66'] 1594 3612 line: cc 5d 4e 26 e2 30 | 1698 | 3612 | 442000a7 | 0 | 1 | 0 | 0 data :[u'cc 5d 4e 26 e2 30', u'1698', u'3612'] mac:[u'cc', u'5d', u'4e', u'26', u'e2', u'30'] [u'cc', u'5d', u'4e', u'26', u'e2', u'30'] 1698 3612 line: 0 26 15 11 7a fa | 1674 | 3612 | 4420008f | 0 | 1 | 0 | 0 data :[u'0 26 15 11 7a fa', u'1674', u'3612'] mac:[u'0', u'26', u'15', u'11', u'7a', u'fa'] [u'0', u'26', u'15', u'11', u'7a', u'fa'] 1674 3612 line: c d2 b5 63 51 87 | 1710 | 3612 | 442000b3 | 0 | 1 | 0 | 0 data :[u'c d2 b5 63 51 87', u'1710', u'3612'] mac:[u'c', u'd2', u'b5', u'63', u'51', u'87'] [u'c', u'd2', u'b5', u'63', u'51', u'87'] 1710 3612 line: data :[u''] mac:[]
Output:
Total number of MAC addresses: 19
will be there in output, however.. number of MAC learn count may be different.

how can we skip those line?

Error:
Traceback (most recent call last): File "ecicollector-1.py", line 42, in <module> mac_addr = '{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac) IndexError: tuple index out of range
Pages: 1 2 3 4 5 6