jeudi 18 novembre 2021

Python - Stopping on first IF condition

I am struggling to understand why my script only matches the first key in the dictionary and doesn't proceed to process other keys.

My script generates the command I need if say only tcp ports are present but if I try to mix this and have other ports(keys) it will not process the other elif. Whether I put this in a for loop or not I seem to be getting the same results. Im new to this so I might have missed something basic here.

ports = {'tcp': [['10', '20']], 'rant': [['100', '200']], 'cm': ['https']}

if __name__ == "__main__":
    obj = []
    tcpport = []
    udpport = []
    mxport = []
    if ports.get("tcp"):
        tcp2 = ports.get("tcp")
        tcpport = True
        for t in compile_port2(tcp2):
            obj.append(t)
            print("compiled tcp only objects")
    elif ports.get("udp"): 
        udpport = True
        udp2 = ports.get("udp")
        for u in compile_port2(udp2):
            obj.append(u)
            print("compiled udp only objects")
    elif ports.get("rant"):
        rant2 = ports.get("rant")
        tcpport = True
        ran_tcp2 = (list(it.zip_longest(*[iter(rant2[0])] * 2)))
        for rt in compile_rant_port2(ran_tcp2):
            obj.append(rt)
            print("compiled tcp only range objects")
    elif ports.get("ranu"):
        ranu2 = ports.get("ranu")
        udpport = True
        ran_udp2 = (list(it.zip_longest(*[iter(ranu2[0])] * 2))) # split strings by groups of 2 for port ranges  
        for ru in compile_rant_port2(ran_udp2):
            obj.append(ru)
            print("compiled udp only range objects")
    elif ports.get("cm"):
        tcpport = True
        cm2 = ports.get("cm")
        for c in compile_port2(cm2):
            obj.append(c)
            print("compiled worded objects\n\n")
    else:
        print("no items found")

Aucun commentaire:

Enregistrer un commentaire