Dec-29-2018, 06:22 PM
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']
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
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() breakPRINT 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