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
#41
please make the last print properly formatted...

print ('It took {:.2f} seconds'.format(exec_time)) #change exec_time to whatever name you use
Reply
#42
Thanks,

print ('It took {:.2f} seconds'.format(exec_time)) #change exec_time to whatever name you use
is to check execution time, and will not be there in final code. Just to calculate time to decide the number threads required to complete 1000+ devices.

I have made some changes in code, sending output to list and taking bport from list as parameter to next command, however I am missing something.

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)
MyList = []
for line in output.decode('utf-8').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('{} {} {} {}'.format(host,mac_address, bport, vlan))
        MyList = host,mac_address, bport, vlan
        #print('{} {} {} {}'.format(*MyList))
        dslam_bport = (MyList[3])
        print(dslam_bport)
        session.write("bportr ".encode('ascii') + dslam_bport +b"\r")
        portoutput = session.read_until("MCR64A >".encode('ascii'), timeout )
        for line in portoutput.decode('utf-8').split('\n'):
            if 'Port:' in line:
                raw_port = line.split(':')
                port = line[1]
                MyList.append(port)
                print('{} {} {} {}'.format(host,mac_address, bport, vlan, port))
facing below error

Error:
Starting Client... Connecting... Sending Commands... Reading... 3612 Traceback (most recent call last): File "ecicollector-1.py", line 52, in <module> session.write("bportr ".encode('ascii') + dslam_bport +b"\r") File "/usr/lib64/python2.7/telnetlib.py", line 279, in write if IAC in buffer: UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
Reply
#43
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)
MyList = []
for line in output.decode('utf-8').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('{} {} {} {}'.format(host,mac_address, bport, vlan))
        MyList = host,mac_address, bport, vlan
        #print('{} {} {} {}'.format(*MyList))
        dslam_bport = MyList[2]
        print(type(dslam_bport))
        command = "bportr " +dslam_bport
        #print(command)
        session.write((command +"\r").encode('ascii'))
        portoutput = session.read_until("MCR64A >".encode('ascii'), timeout )
        for line in portoutput.decode('utf-8').split('\n'):
            if 'Port:' in line:
                raw_port = line.replace(' ','').split(':')
                port = raw_port[1]
                print(port)
                MyList.append(port)
able to print Port details,
Output:
Starting Client... Connecting... Sending Commands... Reading... <type 'unicode'> [b]13[/b]
MyList is List, still showing tuple error

Error:
Traceback (most recent call last): File "ecicollector-1.py", line 62, in <module> MyList.append(port) AttributeError: 'tuple' object has no attribute 'append'
Reply
#44
MyList = []
for line in output.decode('utf-8').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('{} {} {} {}'.format(host,mac_address, bport, vlan))
        MyList = host,mac_address, bport, vlan
        #print('{} {} {} {}'.format(*MyList))
        dslam_bport = MyList[2]
        #print(type(dslam_bport))
        command = "bportr " +dslam_bport
        #print(command)
        session.write((command +"\r").encode('ascii'))
        portoutput = session.read_until("MCR64A >".encode('ascii'), timeout )
        for line in portoutput.decode('utf-8').split('\n'):
            if 'Port:' in line:
                raw_port = line.replace(' ','').split(':')
                port = raw_port[1]
                #print(port)
                MyList = list()
                MyList.append(port)
                #MyList += ((port.decode('utf-8')))
                #data = MyList.append(port)
                #MyList.append(port)
                print('{} {} {} {} {}'.format(host,mac_address, bport, vlan, port))
is this correct way?

Output:
10.117.1.84 E8:37:7A:9D:1A 1616 3612 13 10.117.1.84 54:B8:00:0A:49 1696 3612 53 10.117.1.84 B0:B2:DC:00:05 1730 3612 1 10.117.1.84 28:28:5D:ED:B4 1640 3612 25 10.117.1.84 00:17:7C:76:99 1622 3612 16 10.117.1.84 28:28:5D:94:78 1704 3612 57 10.117.1.84 48:EE:00:0C:95 1630 3612 20 10.117.1.84 48:EE:00:0C:BF 1628 3612 19 10.117.1.84 00:17:7C:80:7C 1732 3612 36 10.117.1.84 E8:CC:18:62:4B 1736 3612 8 10.117.1.84 00:17:7C:61:C2 1596 3612 3 10.117.1.84 00:25:5E:3F:E7 1716 3612 63 10.117.1.84 CC:5D:4E:26:E8 1706 3612 58 10.117.1.84 0C:D2:B5:5F:62 1632 3612 21 10.117.1.84 28:28:5D:F4:00 1680 3612 45 10.117.1.84 CC:5D:4E:26:E2 1698 3612 54 10.117.1.84 00:26:15:11:7A 1674 3612 42 10.117.1.84 0C:D2:B5:63:51 1710 3612 60
Reply
#45
why do you need MyList at all? you don't really use it. Not to mention that it is list, then tuple, then different list...
why do you use .encode('ascii') for strings that are ascii anyway?
why you introduce intermediate variables dslam_bport (you can just use bport) and raw_port (when you can just the slicing at the end of split() line?)
Reply
#46
Thanks Buran, let me work out on this.
Reply
#47
Hi Buran,

changed, let me know any this left for improvement?

for line in output.decode('utf-8').split('\n')[4:]:
           line = line.split('|')
           if len(line) == 8:
              mac, bport, vlan = map(lambda x: x.strip(), line[:3])
              mac_address = mac2norm(mac)
              MyList = host,mac_address, bport, vlan
              command = "bportr " +bport
              session.write((command +"\r").encode('ascii'))
              portoutput = session.read_until("MCR64A >".encode('ascii'), timeout )
              for line in portoutput.split('\n'):
                  if 'Port:' in line:
                      port = line.replace(' ','').split(':')[1]
                      MyList += (port,)
                      print('{} {} {} {} {}'.format(host,mac_address, bport, vlan, port))
working perfectly

tried with another device and output is perfect.

Output:
10.113.1.1 10:62:EB:66:E9 1746 3601 38 10.113.1.1 00:17:7C:6D:FF 1845 3601 20 10.113.1.1 90:8D:78:8B:49 1828 3601 28 10.113.1.1 00:00:0D:48:33 2094 357 34 10.113.1.1 6C:19:8F:5E:BA 1716 3601 24 10.113.1.1 80:26:89:CA:84 2119 3601 44 10.113.1.1 28:28:5D:BF:32 1891 3601 6 10.113.1.1 E8:37:7A:9D:16 1963 3601 16 10.113.1.1 28:28:5D:94:89 1949 3601 9 10.113.1.1 EC:22:80:C4:00 1798 3601 5 10.113.1.1 E4:6F:13:81:00 1969 3601 21 10.113.1.1 E4:6F:13:82:DE 2088 3401 2 10.113.1.1 00:00:0D:48:34 1989 3111 32 10.113.1.1 00:17:7C:7C:3E 1865 3601 31 10.113.1.1 48:EE:00:0C:98 1768 3601 52 10.113.1.1 54:B8:00:0A:41 1712 3601 18 10.113.1.1 00:17:7C:6E:00 2057 3132 10 10.113.1.1 0C:D2:B5:61:CE 1822 3601 15 10.113.1.1 EC:22:80:C4:00 1750 3601 3 10.113.1.1 00:17:7C:6D:C8 2109 3601 23 10.113.1.1 00:17:7C:75:A7 1947 3601 8 10.113.1.1 90:8D:78:84:5C 1983 3125 47 10.113.1.1 80:26:89:C8:EE 2137 3133 37 10.113.1.1 EC:43:F6:7B:22 2090 3401 42 10.113.1.1 E4:6F:13:88:4D 1961 3601 54 10.113.1.1 80:26:89:C7:6D 2133 3142 62 10.113.1.1 00:19:CB:B3:BF 1826 3601 27 10.113.1.1 E4:6F:13:81:00 2021 3601 39 10.113.1.1 00:17:7C:7C:3E 1770 3601 35 10.113.1.1 10:7B:EF:A6:22 1838 3601 53 10.113.1.1 00:17:7C:77:AC 2092 3601 63 10.113.1.1 0C:D2:B5:5E:B4 2116 3601 17
Reply
#48
One more thing. You can align the values to the left or to the right if you know the max length of the value in letters. See my formatting for the mac address values.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#49
Sorry.. Wavic, not able to understand this. However i required this sequence for further processing, output with single space character to split that while inserting to MySQL.

Thanks.
Reply
#50
(Feb-22-2018, 07:27 AM)anna Wrote: changed, let me know any this left for improvement?

Already told you that you don't really use MyList, you just constantly overwrite it. Just remove lines 6 and 13, which don't serve any practical purpose.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  a function to get IP addresses of interfaces Skaperen 2 1,442 May-30-2022, 05:00 PM
Last Post: Skaperen
  Loop through list of ip-addresses [SOLVED] AlphaInc 7 4,007 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,702 Mar-06-2021, 12:50 AM
Last Post: jehoshua
  crunching ip addresses snichols 5 2,827 Nov-10-2020, 05:24 PM
Last Post: snichols
  extract email addresses from gmail vigneshboolog 0 1,779 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,905 Oct-27-2018, 09:33 AM
Last Post: buran
  How do I get coordinates for addresses from the csv list? BigD 6 7,883 Dec-02-2017, 09:38 PM
Last Post: BigD
  How to get data instead of memory addresses KBingo 3 14,262 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