getting back negative results in loop

后端 未结 2 1879
不知归路
不知归路 2021-01-24 04:06

I\'m having trouble pinpointing the exact problem in my code. I have two functions- inrange(), which checks to see if a port is in range, and filt(), w

相关标签:
2条回答
  • 2021-01-24 04:35

    You correctly look for the string sshd line wise, but search the strings TCP and UDP in the whole file. As there is one line with UDP and 123, every call to inrange(file, "UDP", int(my_string)): will return true even if there is a TCP in current line.

    You should extract the proto from the current line. Something like

        proto = newline[:3]
        if (proto in ['UDP', 'TCP']) and inrange(file, proto, int(my_string)):
            print("Line " + str(plusOne) + " is in range" + " " + my_string)    
        else:
            print("Not in range")
    
    0 讨论(0)
  • 2021-01-24 04:44

    Your filt function checks every port it finds against both sets of ranges - I'd suggest rewriting that function to parse out the protocol from the line being read, and pass that variable to inrange along with the port number.

    A small additional note - in the inrange function you're testing the same condition in both the if and elif branches.

    0 讨论(0)
提交回复
热议问题