我想知道我们是否可以使用for循环来执行两个不同的动作

时间:2019-04-24 05:35:07

标签: python python-3.x

我要关闭core1core2并启动core3core4,而我正在研究router1。同样,在处理core1时,我必须调出core2core3并关闭core4router2

router1, router2, = "10.11.12.13", "10.11.12.14"
tunnel1, tunnel2, tunnel3, tunnel4, tunnel5 = 
"tunnel01","tunnel02","tunnel03","tunnel04", "tunnel05"
core1, core2, core3, core4 = "core01", "core02", "core03", "core04"


routers = [router1, router2]
tunnels = [tunnel1, tunnel2]
cores = [core1, core2, core3, core4]
loopback = "loopback"

def noshut(tunnel, router):
    print('NO SHUTDOWN', tunnel, router)

def shut(tunnel, router):
    print('SHUTDOWN', tunnel, router)

for r1 in routers:
    for c in cores:
        #Here I need to shut core1, core2, bring up core3 and core4 in 
        #router 1 and while working on router 2 core3 and core4 should be 
        #down and core1, core2 has to be up. 
        for t1 in tunnels:
            for r2 in routers:
                for t2 in tunnels:
                    if r1 == r2 and t1 == t2:
                        noshut(t1,r1)
                    else:
                        shut(t2,r2)
            print("Ping")
            print("")

这是当前代码的输出:

NO SHUTDOWN tunnel01 10.11.12.13
SHUTDOWN tunnel02 10.11.12.13
SHUTDOWN tunnel01 10.11.12.14
SHUTDOWN tunnel02 10.11.12.14
Ping

SHUTDOWN tunnel01 10.11.12.13
NO SHUTDOWN tunnel02 10.11.12.13
SHUTDOWN tunnel01 10.11.12.14
SHUTDOWN tunnel02 10.11.12.14
Ping

SHUTDOWN tunnel01 10.11.12.13
SHUTDOWN tunnel02 10.11.12.13
NO SHUTDOWN tunnel01 10.11.12.14
SHUTDOWN tunnel02 10.11.12.14
Ping

SHUTDOWN tunnel01 10.11.12.13
SHUTDOWN tunnel02 10.11.12.13
SHUTDOWN tunnel01 10.11.12.14
NO SHUTDOWN tunnel02 10.11.12.14
Ping

我希望它看起来像

SHUTDOWN loopback core01
SHUTDOWN loopback core02
NO SHUTDOWN loopback core03
NO SHUTDOWN loopback core04

NO SHUTDOWN tunnel01 10.11.12.13
SHUTDOWN tunnel02 10.11.12.13
SHUTDOWN tunnel01 10.11.12.14
SHUTDOWN tunnel02 10.11.12.14
Ping

SHUTDOWN tunnel01 10.11.12.13
NO SHUTDOWN tunnel02 10.11.12.13
SHUTDOWN tunnel01 10.11.12.14
SHUTDOWN tunnel02 10.11.12.14
Ping

NO SHUTDOWN loopback core01
NO SHUTDOWN loopback core02
SHUTDOWN loopback core03
SHUTDOWN loopback core04

SHUTDOWN tunnel01 10.11.12.13
SHUTDOWN tunnel02 10.11.12.13
NO SHUTDOWN tunnel01 10.11.12.14
SHUTDOWN tunnel02 10.11.12.14
Ping

SHUTDOWN tunnel01 10.11.12.13
SHUTDOWN tunnel02 10.11.12.13
SHUTDOWN tunnel01 10.11.12.14
NO SHUTDOWN tunnel02 10.11.12.14
Ping

1 个答案:

答案 0 :(得分:0)

您只需要添加一个条件即可关闭/不关闭内核。

router1, router2, = "10.11.12.13", "10.11.12.14"
tunnel1, tunnel2, tunnel3, tunnel4, tunnel5 = "tunnel01","tunnel02","tunnel03","tunnel04", "tunnel05"
core1, core2, core3, core4 = "core01", "core02", "core03", "core04"


routers = [router1, router2]
tunnels = [tunnel1, tunnel2]
cores = [core1, core2, core3, core4]
loopback = "loopback"

def noshut(tunnel, router):
    print('NO SHUTDOWN', tunnel, router)

def shut(tunnel, router):
    print('SHUTDOWN', tunnel, router)

def noshutcore(core):
    print('NO SHUTDOWN loopback', core)

def shutcore(core):
    print('SHUTDOWN loopback', core)


for r1 in routers:
    # conditional for shutting/not shutting cores
    if r1 == router1:
        shutcore(core1)
        shutcore(core2)
        noshutcore(core3)
        noshutcore(core4)
    elif r1 == router2:
        noshutcore(core1)
        noshutcore(core2)
        shutcore(core3)
        shutcore(core4)
    print()
    for t1 in tunnels:
        for r2 in routers:
            for t2 in tunnels:
                if r1 == r2 and t1 == t2:
                    noshut(t1,r1)
                else:
                    shut(t2,r2)
        print()

输出将如下所示。

SHUTDOWN loopback core01
SHUTDOWN loopback core02
NO SHUTDOWN loopback core03
NO SHUTDOWN loopback core04

NO SHUTDOWN tunnel01 10.11.12.13
SHUTDOWN tunnel02 10.11.12.13
SHUTDOWN tunnel01 10.11.12.14
SHUTDOWN tunnel02 10.11.12.14

SHUTDOWN tunnel01 10.11.12.13
NO SHUTDOWN tunnel02 10.11.12.13
SHUTDOWN tunnel01 10.11.12.14
SHUTDOWN tunnel02 10.11.12.14

NO SHUTDOWN loopback core01
NO SHUTDOWN loopback core02
SHUTDOWN loopback core03
SHUTDOWN loopback core04

SHUTDOWN tunnel01 10.11.12.13
SHUTDOWN tunnel02 10.11.12.13
NO SHUTDOWN tunnel01 10.11.12.14
SHUTDOWN tunnel02 10.11.12.14

SHUTDOWN tunnel01 10.11.12.13
SHUTDOWN tunnel02 10.11.12.13
SHUTDOWN tunnel01 10.11.12.14
NO SHUTDOWN tunnel02 10.11.12.14