嵌套 for 循环问题总是使用索引 0

Nested for loop issue always using index 0

我的嵌套循环有问题。我有 ipaddress 和主机名的列表,地址循环是正确的,而主机名循环不是,它总是在中断后使用索引 0。

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

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

输出:

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**

谢谢

您不需要嵌套循环:

for i in range(0,len(ipaddress)):
    print ("Now accessing the device: ",ipaddress[i])
    dev = ipaddress[i].strip()
    tn = telnetlib.Telnet(dev)
    print("Host: ",dev)
    tn.read_until(b"Username:")
    tn.write(user.encode("ascii") + b"\n")

    print ("Hostname :", hostname[i])
    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 " + hostname[i] + "\n").encode('ascii'))
    time.sleep(2)
    tn.write(b"exit \n")
    tn.write(b"wr mem \n")
    tn.close()

您在 nhostname 循环的末尾放置了一个 break 语句,这意味着嵌套循环在第一次迭代后中断并返回到 ipadress 循环。

删除嵌套循环末尾的 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()
    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

你的内循环中断了,但在外循环的第二个循环中,它又重新开始了。这就是为什么您的主机名永远不会改变的原因。

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()