-1

I want to shutdown core1, core2 and bring up core3, core4 while I am working on router1. In the same way I have to bring up core1, core2 and shutdown core3, core4 while working on router2.

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

This is the output of present code:

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

I want it to look like

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
Josh21
  • 506
  • 5
  • 15
  • You might want to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) for that or look at [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Apr 24 '19 at 05:57
  • You just need to add a conditional for shutting/non shutting down cores, check my answer below! – Devesh Kumar Singh Apr 24 '19 at 06:13

1 Answers1

0

You just need to add a conditional for shutting/non shutting down cores.

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

The output will then look like.

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
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40