Help required to print MAC addresses - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help required to print MAC addresses (/thread-8351.html) |
RE: Help required to print MAC addresses - buran - Feb-20-2018 please make the last print properly formatted... print ('It took {:.2f} seconds'.format(exec_time)) #change exec_time to whatever name you use RE: Help required to print MAC addresses - anna - Feb-21-2018 Thanks, print ('It took {:.2f} seconds'.format(exec_time)) #change exec_time to whatever name you useis 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
RE: Help required to print MAC addresses - anna - Feb-21-2018 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, MyList is List, still showing tuple error
RE: Help required to print MAC addresses - anna - Feb-21-2018 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?
RE: Help required to print MAC addresses - buran - Feb-21-2018 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?) RE: Help required to print MAC addresses - anna - Feb-21-2018 Thanks Buran, let me work out on this. RE: Help required to print MAC addresses - anna - Feb-22-2018 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.
RE: Help required to print MAC addresses - wavic - Feb-22-2018 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. RE: Help required to print MAC addresses - anna - Feb-22-2018 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. RE: Help required to print MAC addresses - buran - Feb-22-2018 (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. |