Python Forum

Full Version: Nested for loop issue always using index 0
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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:
@ichabod801, Thanks Man... Got it. Its now working.

Have a great holiday. :)