Python Forum
Nested for loop issue always using index 0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested for loop issue always using index 0
#1
I'm having an issue with my nested loop wherein I have the list of ipaddress and hostname, looping of address is correct while looping on hostname is not, it always use the index 0 after the break.

list example: ipaddress = ['192.168.1.1', '192.168.1.2']
hostname = ['lab-sw01', 'lab-rtr02']

import csv
import telnetlib
import getpass
import sys
import time

def main():
    

#open csv file, then put to variable csvfile.
    with open(input("Input the CSV filename: ")) as csvfile:
##    with open(input("Input the CSV filename: ")) as csvfile:
        riphostCSV = csv.reader(csvfile, delimiter=',')
        
#Put the data as list from excel.
        ipaddress = []
        nhostname = []

#Action - Put the data from excel to variable[list]
        for col in riphostCSV:
            ipadr = col[0]
            nhost = col[1]

            ipaddress.append(ipadr)
            nhostname.append(nhost)
        print (nhostname)
# Loging Session            
        user = input("Enter your username: ")
        password=getpass.getpass("Enter Your Password Here: ")
        
# Accessing the device       
        for i in ipaddress:
            print ("Now accessing the device: ", i)
            dev = i.strip()
            tn = telnetlib.Telnet(dev)
            print("Host: ",dev)
            tn.read_until(b"Username:")
            tn.write(user.encode("ascii") + b"\n")

            for j in nhostname:
                print ("Hostname :", j)
    ##            hn = i.strip() 
                if password:
                    tn.read_until(b"Password: ")
                    tn.write(password.encode("ascii") + b"\n")
                tn.write(b"conf t\r\n")
                time.sleep(2)
                tn.write(("hostname " + j + "\n").encode('ascii'))
                time.sleep(2)
                tn.write(b"exit \n")
                tn.write(b"wr mem \n")
                tn.close()
                break
PRINT OUTPUT:
Now accessing the device: 192.168.137.50
Host: 192.168.137.50
Hostname : lab-sw01
Now accessing the device: 192.168.137.51
Host: 192.168.137.51
Hostname : lab-sw01

thanks
Reply
#2
The break ensures that the loop will always stop after the first iteration, which will always be the first item in the list. If you want to get to the second item, you need to remove the break.

But looking at your code makes me wonder if you want the the second hostname to go with the second IP address? If so, you can zip the two lists together:

for ipaddy, nhost in zip(ipaddress, nhostname):
Of course, since you are building the lists yourself, you could just build a list of paired tuples:

        sites = []

        for col in riphostCSV:
            ipadr = col[0]
            nhost = col[1]
            sites.append((ipadr, nhost))
        ...
        for ipadr, nhost in sites:
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
@ichabod801, Thanks Man... Got it. Its now working.

Have a great holiday. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Need help on this nested loop task PP9044 4 4,598 Apr-16-2021, 01:31 PM
Last Post: perfringo
  Nested if stmts in for loop johneven 2 5,836 Oct-19-2019, 04:05 AM
Last Post: xeedon
  nested while loop flow help Ponamis 4 2,920 Nov-02-2018, 11:22 PM
Last Post: Ponamis
  Nested loop Tryhard20 3 5,656 Sep-05-2018, 04:57 AM
Last Post: volcano63
  having an issue with nested if statements fad3r 6 4,589 May-08-2018, 12:25 AM
Last Post: ljmetzger
  Nested Loop multiplication table SushiRolz 3 10,153 Feb-28-2018, 04:34 AM
Last Post: Larz60+
  Nested Loop to Generate Triangle Babbare 12 11,605 May-29-2017, 05:00 AM
Last Post: buran

Forum Jump:

User Panel Messages

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