May-18-2019, 10:37 AM
Hi All,
Below is the my function, which telnet to devices post checking reachability, after login, I am gathering interface status and if interface name start with Fa0 and status in 'up', storing to dict, later appended to list.
However post checking interface and status, its printing correctly, but list is printing incorrect details.
Below is the my function, which telnet to devices post checking reachability, after login, I am gathering interface status and if interface name start with Fa0 and status in 'up', storing to dict, later appended to list.
However post checking interface and status, its printing correctly, but list is printing incorrect details.
def open_telnet_new(reachable_sw): timeout = 120 sr_no = 0 switch_port_dict = {} interface_list = [] try: session = telnetlib.Telnet(reachable_sw, 23, timeout) # session.set_debuglevel(2) time.sleep(1) session.read_until(b"Username:") session.write((user+"\r").encode('ascii')) time.sleep(2) session.read_until(b"Password:",2) session.write((password + "\r").encode('ascii')) time.sleep(2) session.read_until(b">") session.write("term len 0".encode('ascii') + b"\r") session.read_until(b">") session.write("show int desc".encode('ascii') + b"\r") #output = session.read_all() output = session.read_until(">".encode('ascii'), timeout ) #print(type(output)) for line in output.decode('utf-8').split('\n')[2:]: if '>' in line: continue else: newline = re.sub('\s{2,}',' ',line).replace('admin down','admin_down').strip().split(' ') interface = newline[0] status = newline[1] if interface.startswith('Fa0') and status == 'up': print(reachable_sw,interface,status) #if status =='up': #print('yes') switch_port_dict['switch_ip'] = reachable_sw switch_port_dict['port'] = interface interface_list.append(switch_port_dict) #interface_list.append(reachable_sw) #print(reachable_sw,interface_list) else: continue for interfaces in interface_list: #for ports in interfaces: print(interfaces['switch_ip'],interfaces['port'])output while printing list which is incorrect.
Output:172.21.212.67 Fa0/22
172.21.212.67 Fa0/22
172.21.212.67 Fa0/22
172.21.241.227 Fa0/24
172.21.241.227 Fa0/24
172.21.241.227 Fa0/24
172.21.241.227 Fa0/24
172.21.226.83 Fa0/24
172.21.226.83 Fa0/24
172.21.226.83 Fa0/24
172.21.226.83 Fa0/24
172.21.236.99 Fa0/24
172.21.236.99 Fa0/24
172.21.236.99 Fa0/24
172.21.236.99 Fa0/24
172.21.236.99 Fa0/24
172.21.236.99 Fa0/24
172.21.236.99 Fa0/24
172.21.212.51 Fa0/23
172.21.212.51 Fa0/23
172.21.202.51 Fa0/22
172.21.202.51 Fa0/22
correct output without list.Output:172.21.236.99 Fa0/1 up
172.21.236.99 Fa0/2 up
172.21.236.99 Fa0/7 up
172.21.236.99 Fa0/8 up
172.21.236.99 Fa0/22 up
172.21.236.99 Fa0/23 up
172.21.236.99 Fa0/24 up
172.21.241.227 Fa0/17 up
172.21.241.227 Fa0/21 up
172.21.241.227 Fa0/23 up
172.21.241.227 Fa0/24 up
172.21.212.67 Fa0/18 up
172.21.212.67 Fa0/19 up
172.21.212.67 Fa0/22 up
172.21.226.83 Fa0/5 up
172.21.226.83 Fa0/21 up
172.21.226.83 Fa0/23 up
172.21.226.83 Fa0/24 up
172.21.202.51 Fa0/19 up
172.21.202.51 Fa0/22 up
172.21.212.51 Fa0/20 up
172.21.212.51 Fa0/23 up