Вам нужно что-то вроде
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i,j in zip(ipaddress,nhostname):
print ("Now accessing the device: ", i)
dev = i.strip()
print("Host: ",dev)
print("hostname " + j + "\n")
Вывод:
Now accessing the device: 192.168.1.1
Host: 192.168.1.1
hostname lab-sw01
Now accessing the device: 192.168.1.2
Host: 192.168.1.2
hostname lab-rtr02
Ваш внутренний цикл разрывается, но во втором цикле внешнего цикла for он начинается заново,Вот почему ваше имя хоста никогда не меняется.
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i in ipaddress:
print ("Now accessing the device: ", i)
print("Host: ",dev)
for j in nhostname: #In every looping of the outer loop it will access the first element of the nhostname
print ("Hostname :", j)
break
Я думаю, ваш код должен быть -
ipaddress = ['192.168.1.1', '192.168.1.2']
nhostname = ['lab-sw01', 'lab-rtr02']
for i,j in zip(ipaddress,nhostname):
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")
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()