Posts: 2,953
Threads: 48
Joined: Sep 2016
Did you see my last post? We split the 'output' by new line symbol so every line will be a single element from the list. We cut it from the fifth line with index 4 up to the index -2. This part of the for loop expression: [4:-2]
Posts: 221
Threads: 71
Joined: Dec 2017
code changed as per your suggestion, able to see mac captured, however not able to print in tuple
for line in output.decode('utf-8').split('\n')[4:-2]:
#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)) 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: 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 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
Posts: 2,953
Threads: 48
Joined: Sep 2016
In the last line you are printing the 'mac' variable. You have to replace it with the 'mac_addr' properly formated before that.
Posts: 8,168
Threads: 160
Joined: Sep 2016
Oh, obviously you have empty line at the end of the file. Just check if line has anything in it before processing it.
And really, you need to put more effort, this is really obvious (or should be for someone who produced rest of the code)
Posts: 221
Threads: 71
Joined: Dec 2017
Wavic, Buran.... thanks for you valuable time and support and encouragement to learn python.
I am trying to parse output through below code, is this good way? but still stuck
data = iter(output.split('\n'))
try:
for line in data:
if "dishash all" in line:
#skipping line with dishash all command
next(data)
if line == ' ':
#skipping blank line
next(data)
else:
for line in data.split('\n')[4:-2]:
data = [field.strip() for field in line.split('|')[:3]]
mac = [elm for elm in data[0].split() if elm]
if line == '':
#skipping last blank line
next(data)
if 'Total' in line:
#skipping line with Total number of MAC addresses
next(data)
else:
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))
except StopIteration: Error: Traceback (most recent call last):
File "ecicollector-1.py", line 44, in <module>
for line in data.split('\n')[4:-2]:
AttributeError: 'listiterator' object has no attribute 'split'
Posts: 8,168
Threads: 160
Joined: Sep 2016
Feb-20-2018, 11:16 AM
(This post was last modified: Feb-20-2018, 11:17 AM by buran.)
dude, why you need to over complicate things? you open a file, file handler You have object returned by output.split() that supports iterator protocol. Why the hell you need to create another iterator?
Also using next() within for item in iterator will just screw things...
You turn these several lines of code that wavic provided into a mess
Posts: 221
Threads: 71
Joined: Dec 2017
ooops... Sorry.. Buran... will go with Wavic code.
Thanks
Posts: 8,168
Threads: 160
Joined: Sep 2016
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))
Posts: 2,953
Threads: 48
Joined: Sep 2016
Finally!
That misunderstanding with the what is in the output made the topic four pages
Posts: 221
Threads: 71
Joined: Dec 2017
Thanks Buran and Wavic...
Thanks ton.. its working.You've made my day
Output: Starting Client...
Connecting...
Sending Commands...
Reading...
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
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
CC:5D:4E:26:E2 | 1698 | 3612
00:26:15:11:7A | 1674 | 3612
0C:D2:B5:63:51 | 1710 | 3612
('It took', 5.37320613861084, 'seconds.')
|