嵌套循环问题始终使用索引0

时间:2018-12-29 18:14:29

标签: python python-3.x loops for-loop network-programming

我的嵌套循环有问题。我有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**

谢谢

3 个答案:

答案 0 :(得分:1)

您不需要嵌套循环:

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

答案 1 :(得分:1)

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

在嵌套循环的末尾删除break语句,它应该可以工作。

答案 2 :(得分:1)

您需要类似的东西

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