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
#11
As you can see the code split the output by new line symbol then gets all elements from the third one with index 2 to the end. No way the error is because of the first line.

Is the corrected code working for you? And the last printed summarize?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#12
sorry.. i am on the same page, below is the error
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 > Traceback (most recent call last): File "ecicollector.py", line 43, in <module> mac_addr, bport, vlan = [column.strip() for column in output.split('\n')[0].split('|')[:3]] ValueError: need more than 1 value to unpack

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()
    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))
print("Done")
print 'It took', time.time()-start, 'seconds.'
is my code ok?
Reply
#13
My bad :)
mac_addr, bport, vlan = list([column.strip() for column in output.split('\n')[0].split('|')[:3]])
I didn't get these errors. Dodgy
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#14
mac len output as below... going mad
Output:
dishash all mac address | bport | vlan | flow agg |static|aging|proccess|deny_sa ========================================================================== 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 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 > 2 1 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 0 6 0 2 Done It took 5.51255607605 seconds.

meanwhile.. I am working on another script to print mac addresses from another device, everything going well, however not able to skip match line.

import xlwt
import sys
import os
import xlrd
import unidecode
import telnetlib
import time
import re
from time import sleep
start = time.time()
user = 'admin'
password = '1234'
print ("Starting Client...")
host    = "10.19.1.49"
timeout = 2
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(">")
    print("Sending Commands...")
    session.write("dsl -c".encode('ascii') + b"\r")
    print("Reading...")
    session.read_until("$", timeout )
    session.write('get bridge forwarding' +'\r\r')
    time.sleep(2)
    output = session.read_until("$", timeout )
    session.write('\x1A')
    session.read_until(">")
    session.write("exit"+"\r")
    session.close()
    data = iter(output.strip().split('\n'))
    try:
        for item in data:
            if "get" in item:
               next(data)
            if "MAC" in item:
               next(data)
            if "----" in item:
               next(data)
            print item
    except StopIteration:
           pass
file.close()
print("Done")
print 'It took', time.time()-start, 'seconds.'
current output...
Output:
get bridge forwarding ----------------------------------------------- 00:18:82:2B:F4:A2 193 662 Learned C0:A0:BB:9C:38:21 22 662 Learned 00:90:1A:A3:C6:23 193 822 Learned E8:37:7A:9D:0F:44 15 822 Learned 00:90:1A:A3:C6:24 193 3214 Learned E8:CC:18:60:B3:E4 17 3214 Learned 00:90:1A:A3:C6:24 193 3240 Learned C0:EA:E4:44:81:A1 1 3240 Learned 00:17:7C:77:E6:DE 14 3603 Learned 10:62:EB:66:E8:2E 3 3603 Learned 40:7C:7D:1F:89:73 193 3603 Learned CC:5D:4E:72:77:C0 10 3603 Learned $
expected output

Output:
00:18:82:2B:F4:A2 193 662 Learned C0:A0:BB:9C:38:21 22 662 Learned 00:90:1A:A3:C6:23 193 822 Learned E8:37:7A:9D:0F:44 15 822 Learned 00:90:1A:A3:C6:24 193 3214 Learned E8:CC:18:60:B3:E4 17 3214 Learned 00:90:1A:A3:C6:24 193 3240 Learned C0:EA:E4:44:81:A1 1 3240 Learned 00:17:7C:77:E6:DE 14 3603 Learned 10:62:EB:66:E8:2E 3 3603 Learned 40:7C:7D:1F:89:73 193 3603 Learned CC:5D:4E:72:77:C0 10 3603 Learned
Reply
#15
I copied a wrong loop.
In [2]: 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 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("{:^23}\t{:^5}\t{:^5}".format(mac_addr, bport, vlan))
Here is what I get. Try it.
Output:
mac address bport vlan e8:37:7a:9d:1a:f1 1616 3612 54:b8:0a:49:97:d5 1696 3612 b0:b2:dc:05:0d:87 1730 3612 00:16:fa:d0:be:a1 300 4094 28:28:5d:ed:b4:41 1640 3612 00:17:7c:76:99:4d 1622 3612 28:28:5d:94:78:da 1704 3612 48:ee:0c:95:08:ee 1630 3612 48:ee:0c:bf:49:65 1628 3612 00:17:7c:80:7c:4a 1732 3612 e8:cc:18:62:4b:d3 1736 3612 00:25:5e:3f:e7:18 1716 3612 cc:5d:4e:26:e8:e2 1706 3612 0c:d2:b5:5f:62:6e 1632 3612 28:28:5d:f4:0f:dc 1680 3612 e8:37:7a:9d:0e:6b 1734 3612 cc:5d:4e:26:e2:30 1698 3612 00:26:15:11:7a:fa 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
#16
import xlwt
import sys
import os
import xlrd
import unidecode
import telnetlib
import time
import re
from time import sleep
user = 'user removed'
password = 'password removed'
start = time.time()
print ("Starting Client...")
host    = "host removed"
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()
    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 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("{:^23}\t{:^5}\t{:^5}".format(mac_addr, bport, vlan))
print("Done")
print 'It took', time.time()-start, 'seconds.'
Output:
Starting Client... Connecting... Sending Commands... Reading... dishash all mac address | bport | vlan | flow agg |static|aging|proccess|deny_sa ========================================================================== 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 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 c d2 b5 63 51 87 | 1710 | 3612 | 442000b3 | 0 | 1 | 0 | 0 Total number of MAC addresses: 18 MCR64A > Traceback (most recent call last): File "ecicollector.py", line 37, in <module> mac_addr, bport, vlan = [column.strip() for column in output.split('\n')[0].split('|')[:3]] ValueError: need more than 1 value to unpack
sorry to trouble you, but same error.
Reply
#17
https://python-forum.io/Thread-Help-requ...4#pid39924
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#18
python3.6 showing below error, I am fail to understand issue

Output:
Reading... b'dishash all\r\r\n\r\n mac address | bport | vlan | flow agg |static|aging|proccess|deny_sa\r\n==========================================================================\r\n e8 37 7a 9d 1a f1 | 1616 | 3612 | 44200059 | 0 | 1 | 0 | 0\r\n 54 b8 a 49 97 d5 | 1696 | 3612 | 442000a5 | 0 | 1 | 0 | 0\r\n b0 b2 dc 5 d 87 | 1730 | 3612 | 442000c7 | 0 | 1 | 0 | 0\r\n 0 16 fa d0 be a1 | 300 | 4094 | 4420002c | 1 | 0 | 0 | 0\r\n 28 28 5d ed b4 41 | 1640 | 3612 | 4420006f | 0 | 1 | 0 | 0\r\n 0 17 7c 76 99 4d | 1622 | 3612 | 4420005d | 0 | 1 | 0 | 0\r\n 28 28 5d 94 78 da | 1704 | 3612 | 442000ad | 0 | 1 | 0 | 0\r\n 48 ee c 95 8 ee | 1630 | 3612 | 44200065 | 0 | 1 | 0 | 0\r\n 48 ee c bf 49 65 | 1628 | 3612 | 44200063 | 0 | 1 | 0 | 0\r\n 0 17 7c 80 7c 4a | 1732 | 3612 | 442000c9 | 0 | 1 | 0 | 0\r\n e8 cc 18 62 4b d3 | 1736 | 3612 | 442000cd | 0 | 1 | 0 | 0\r\n 0 17 7c 61 c2 c2 | 1596 | 3612 | 44200047 | 0 | 1 | 0 | 0\r\n cc 5d 4e 26 e8 e2 | 1706 | 3612 | 442000af | 0 | 1 | 0 | 0\r\n c d2 b5 5f 62 6e | 1632 | 3612 | 44200067 | 0 | 1 | 0 | 0\r\n 28 28 5d f4 f dc | 1680 | 3612 | 44200095 | 0 | 1 | 0 | 0\r\n 6c 72 20 ee a8 66 | 1594 | 3612 | 44200045 | 0 | 1 | 0 | 0\r\n e8 37 7a 9d e 6b | 1734 | 3612 | 442000cb | 0 | 1 | 0 | 0\r\n cc 5d 4e 26 e2 30 | 1698 | 3612 | 442000a7 | 0 | 1 | 0 | 0\r\n c d2 b5 63 51 87 | 1710 | 3612 | 442000b3 | 0 | 1 | 0 | 0\r\n\n\rTotal number of MAC addresses: 19\n\r\r\nMCR64A >' Traceback (most recent call last): File "ecicollector-1.py", line 35, in <module> mac_addr, bport, vlan = [column.strip() for column in output.split('\n')[0].split('|')[:3]] TypeError: a bytes-like object is required, not 'str'
Reply
#19
I see that your output is as bytes. The new line is the Windows one ( \r\n ).

You have to convert it to string.

b'hello'.decode('utf-8')
So
output = output.decode('utf-8')
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#20
Thanks Wavic, Printing issue is resolved, however still issue with parsing, below is the error.

Output:
Traceback (most recent call last): File "ecicollector-1.py", line 35, in <module> mac_addr, bport, vlan = [column.strip() for column in output.split('\n')[0].split('|')[:3]] TypeError: a bytes-like object is required, not 'str'

current error
Output:
Traceback (most recent call last): File "ecicollector-1.py", line 35, in <module> mac_addr, bport, vlan = [column.strip() for column in output.decode('utf-8').split('\n')[0].split('|')[:3]] ValueError: not enough values to unpack (expected 3, got 1)
Reply


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