Python Forum
Help required to print MAC addresses
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help required to print MAC addresses
#21
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#22
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
Reply
#23
Well, print 'mac' to see what it holds.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#24
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
Reply
#25
Put a print above the error line to see what 'mac' variable contains.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#26
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
Reply
#27
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:]:
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#28
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
Reply
#29
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]:
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#30
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a function to get IP addresses of interfaces Skaperen 2 1,419 May-30-2022, 05:00 PM
Last Post: Skaperen
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 3,953 May-11-2022, 02:23 PM
Last Post: menator01
  instance methods sharing addresses mim 1 2,235 Mar-28-2021, 05:22 AM
Last Post: deanhystad
  Convert email addresses to VCF format jehoshua 2 4,667 Mar-06-2021, 12:50 AM
Last Post: jehoshua
  crunching ip addresses snichols 5 2,771 Nov-10-2020, 05:24 PM
Last Post: snichols
  extract email addresses from gmail vigneshboolog 0 1,759 Feb-11-2020, 09:23 AM
Last Post: vigneshboolog
  Extract email addresses from string and store them in a list oslosurfer 2 2,688 Nov-24-2019, 03:35 PM
Last Post: oslosurfer
  Read list of IP addresses from file and return a network dflick 4 4,880 Oct-27-2018, 09:33 AM
Last Post: buran
  How do I get coordinates for addresses from the csv list? BigD 6 7,823 Dec-02-2017, 09:38 PM
Last Post: BigD
  How to get data instead of memory addresses KBingo 3 14,210 Jun-12-2017, 12:36 AM
Last Post: KBingo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020