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
#1
Hi There,

I am getting below MAC details from telnet script, I am not able print in MAC address format, please help.


mac address | bport | vlan | flow agg |static|aging|proccess|deny_sa
==========================================================================
0 16 fa d0 be a1 | 300 | 4094 | 4420002c | 1 | 0 | 0 | 0
0 17 7c 76 99 4d | 1622 | 3612 | 4420005d | 0 | 1 | 0 | 0
0 17 7c 80 7c 4a | 1732 | 3612 | 442000c9 | 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

expecting output as, it seems this devices is not printing leading Zero in MAC address.

00:16:fa:d0:be:a1
00:17:7c:76:99:4d
00:17:7c:61:c2:c2
00:25:5e:3f:e7:18
Reply
#2
output = """mac address | bport | vlan | flow agg |static|aging|proccess|deny_sa
==========================================================================
0 16 fa d0 be a1 | 300 | 4094 | 4420002c | 1 | 0 | 0 | 0
0 17 7c 76 99 4d | 1622 | 3612 | 4420005d | 0 | 1 | 0 | 0
0 17 7c 80 7c 4a | 1732 | 3612 | 442000c9 | 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"""

for line in output.split('\n')[2:]:
    mac = line.split('|')[0].strip()
    print('{:0>17}'.format(':'.join(mac.split())))
Output:
00:16:fa:d0:be:a1 00:17:7c:76:99:4d 00:17:7c:80:7c:4a 00:17:7c:61:c2:c2 00:25:5e:3f:e7:18
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Thanks... some how.. its not working with some below outputs...


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
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 | 0 | 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


thanks is advance.

Bold entries

e8:37:7a:9d:1a:f1
054:b8:a:49:97:d5
00b0:b2:dc:5:d:87
00:16:fa:d0:be:a1
00:17:7c:76:99:4d
0048:ee:c:95:8:ee
048:ee:c:bf:49:65
00:17:7c:80:7c:4a
e8:cc:18:62:4b:d3
00:17:7c:61:c2:c2
00:25:5e:3f:e7:18
cc:5d:4e:26:e8:e2
0c:d2:b5:5f:62:6e
028:28:5d:f4:f:dc
0e8:37:7a:9d:e:6b
cc:5d:4e:26:e2:30
00:26:15:11:7a:fa
0c:d2:b5:63:51:87
Reply
#4
for line in output.split('\n')[2:]:
    mac = line.split('|')[0].strip().split()
    print('{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Thanks wavic, let me try and come back to you.

regards
Anna
Reply
#6
my bad, something is going wrong


import xlwt
import sys
import os
import xlrd
import unidecode
import telnetlib
import time
import re
from time import sleep
user = 'eciecidslam'
password = 'Hi-FOCuS'
start = time.time()
print ("Starting Client...")
host    = "10.117.1.84"
timeout = 120

print ("Connecting...")
try:
    session = telnetlib.Telnet(host, 23, timeout)
    time.sleep(1)
    session.read_until("Login :",2)
    session.write(user+"\r")
    time.sleep(2)
    session.read_until("Password :",2)
    session.write(password + "\r")
except Exception,e:
    print ("socket timeout")
else:
    time.sleep(2)
    session.read_until("MCR64A >")
    print("Sending Commands...")
    session.write("dishash all".encode('ascii') + b"\r")
    print("Reading...")
    output = session.read_until("MCR64A >", timeout )
    session.write("logout"+"\r")
    #print (output)
    session.close()
    for line in output.split('\n')[2:]:
         mac = line.split('|')[0].strip().split()
         print('{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac))
print("Done")
print 'It took', time.time()-start, 'seconds.'
getting below error

Traceback (most recent call last):
  File "ecicollector.py", line 40, in <module>
    print('{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac))
IndexError: tuple index out of range
Reply
#7
There is something in the output that I am not aware of.

What this loop is doing is to split each line after the header and the '====...' by '|' separator, get the first element which is the mac address, split the ( six ) numbers and print each one formatted. It's quite simple. Does it print something before throwing the error? Where does it stop? What is on the line after that? As I see the error there is a wrong number of the elements of the mac address. So the print placeholders are six but in the 'mac' list could be any number. Put a print(len(mac)) before the actual mac address printing to see what is happening.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
Hi Wavic,

This is output from script

Starting Client...
Connecting...
Sending Commands...
Reading...
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   |  1  |   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 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

Total number of MAC addresses: 19

MCR64A >
Traceback (most recent call last):
  File "ecicollector.py", line 40, in <module>
    print('{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac))
IndexError: tuple index out of range
actually... i want to print this like below
MAC bport vlan
e8:37:7a:9d:1a:f1 1616 3612

to find actual interface of the mac i need to fire another command to check i.e.
MCR64A >bportr 1616

         Bridge Port Data Of Port ID: 1616
--------------------------------------------
VP:                       0
VC:                       33
MANI:                     A
Shelf:                    0
Slot:                     7
[b]Port:                     13[/b]
UNI:                      1
Layer:                    0
PVID:                     3612
Ingress vlan:             65535
Default user priority:    7
Acceptable frame types:   1
Max ingress priority:     7
Row status:               active(1)
Oper status:              up(1)
Oper down reason:         0
Actual failure:           noFailure(1)
Req user defined filters: 0
Act user defined filters: 0
Max learned addresses:    8
Fail reason:              1
Enforce def priority:     disable(2)
Max DHCP addresses:       3
Sensed protocol:          irrelevant(1)
Port remote id:
Port circuit id:
Req. vlinks:              1073741824 Single
Actual vlinks:            1073741824 Single
 
and final output will be 
MAC                bport   vlan  port
e8:37:7a:9d:1a:f1  1616    3612  13
Reply
#9
I can't reproduce the error you get:
In [1]: 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
   ...:  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 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"""

In [2]: for line in output.split('\n')[2:]:
   ...:     mac = line.split('|')[0].strip().split()
   ...:     print(len(mac))
   ...:     print('{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac))
   ...:     
6
e8:37:7a:9d:1a:f1
6
54:b8:0a:49:97:d5
6
b0:b2:dc:05:0d:87
6
00:16:fa:d0:be:a1
6
28:28:5d:ed:b4:41
6
00:17:7c:76:99:4d
6
28:28:5d:94:78:da
6
48:ee:0c:95:08:ee
6
48:ee:0c:bf:49:65
6
00:17:7c:80:7c:4a
6
e8:cc:18:62:4b:d3
6
00:25:5e:3f:e7:18
6
cc:5d:4e:26:e8:e2
6
0c:d2:b5:5f:62:6e
6
28:28:5d:f4:0f:dc
6
e8:37:7a:9d:0e:6b
6
cc:5d:4e:26:e2:30
6
00:26:15:11:7a:fa
6
0c:d2:b5:63:51:87

In [3]: for line in output.split('\n')[2:]:
   ...:     address = line.split('|')[0].strip().split()
   ...:     mac = [elm for elm in address if elm]
   ...:     print('{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}:{:0>2}'.format(*mac))
   ...:     
e8:37:7a:9d:1a:f1
54:b8:0a:49:97:d5
b0:b2:dc:05:0d:87
00:16:fa:d0:be:a1
28:28:5d:ed:b4:41
00:17:7c:76:99:4d
28:28:5d:94:78:da
48:ee:0c:95:08:ee
48:ee:0c:bf:49:65
00:17:7c:80:7c:4a
e8:cc:18:62:4b:d3
00:25:5e:3f:e7:18
cc:5d:4e:26:e8:e2
0c:d2:b5:5f:62:6e
28:28:5d:f4:0f:dc
e8:37:7a:9d:0e:6b
cc:5d:4e:26:e2:30
00:26:15:11:7a:fa
0c:d2:b5:63:51:87
However the correction in the last loop in the above should eliminate it.

In [4]: mac_addr, bport, vlan = [column.strip() for column in output.split('\n')[0].split('|')[:3]]
   ...: print("{:^23}\t{:^5}\t{:^5}".format(mac_addr, bport, vlan))
   ...: print()
   ...: for line in output.split('\n')[2:]:
   ...:     data = [field.strip() for field in line.split('|')[:3]]
   ...:     mac = [elm for elm in address if elm]
   ...:     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_addr, bport, vlan))
   ...:     
Output:
mac address bport vlan 0c:d2:b5:63:51:87 1616 3612 0c:d2:b5:63:51:87 1696 3612 0c:d2:b5:63:51:87 1730 3612 0c:d2:b5:63:51:87 300 4094 0c:d2:b5:63:51:87 1640 3612 0c:d2:b5:63:51:87 1622 3612 0c:d2:b5:63:51:87 1704 3612 0c:d2:b5:63:51:87 1630 3612 0c:d2:b5:63:51:87 1628 3612 0c:d2:b5:63:51:87 1732 3612 0c:d2:b5:63:51:87 1736 3612 0c:d2:b5:63:51:87 1716 3612 0c:d2:b5:63:51:87 1706 3612 0c:d2:b5:63:51:87 1632 3612 0c:d2:b5:63:51:87 1680 3612 0c:d2:b5:63:51:87 1734 3612 0c:d2:b5:63:51:87 1698 3612 0c:d2:b5:63:51:87 1674 3612 0c:d2:b5:63:51:87 1710 3612
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
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 | 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 Total number of MAC addresses: 19 MCR64A >
I think first line of output and last two line are creating problem.
Reply


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