Python Forum
list is printing incorrectly..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list is printing incorrectly..
#1
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.


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
Reply
#2
You are appending the same dictionary every time. Unless you do an explicit copy of a dictionary, whenever you modify the dictionary, you modify every other instance of the dictionary you left laying about. That's because the variables are just pointing to the (same) dictionary, not hosting independent versions of it.

I would just condense lines 34-36 into one line: interface_list.append({'switch_ip': reachable_sw, 'port': interface}).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Printing through list.. again jesse68 2 1,139 Apr-16-2022, 03:24 PM
Last Post: jesse68
  help for list printing jip31 8 3,616 May-01-2021, 03:52 AM
Last Post: Pedroski55
  Writing to file ends incorrectly project_science 4 2,679 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Problem printing last element from a list tester_V 3 2,374 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Printing empty list? hhydration 2 2,109 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Printing images from a list Heyjoe 4 2,811 Jun-22-2020, 02:28 AM
Last Post: Heyjoe
  New to the language. What am I doing incorrectly here? christopher3786 3 2,232 Jun-20-2020, 10:18 AM
Last Post: pyzyx3qwerty
  my openpyxl use is too slow, am I reading rows incorrectly? Clunk_Head 2 8,286 Apr-30-2020, 10:29 PM
Last Post: deac33
  printing a list contents without brackets in a print statement paracelx 1 2,115 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  Printing List in one line bharat_s579 6 4,116 May-26-2019, 08:30 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020