1

I currently have a program that scans for open ports on your computer, and appends them to a list (systemPorts). I want to compare systemPorts to another list (vulnerableports) and append those that match to a 3rd list called comparedPorts.

vulnerablePorts = ['21','22','110', etc]
systemPorts = ['22', '80']
comparedPorts = ['22']

I'm not new to python but I've never written something that searches a list for a list of items.

Thank you!

7 Answers7

9

You can use list comprehension:

vulnerablePorts = ['21','22','110']
systemPorts = ['22', '80']
comparedPorts = [p for p in vulnerablePorts if p in systemPorts]
print(comparedPorts)  # ['22']

Or use & operator:

comparedPorts = list(set(vulnerablePorts) & set(systemPorts))
print(comparedPorts)  # ['22']

Or use intersection command: (command for python set)

comparedPorts = list(set(vulnerablePorts).intersection(systemPorts))
print(comparedPorts)  # ['22']
Aviad Levy
  • 750
  • 4
  • 13
4

You can use the set data structure of Python. Just convert both the lists into set and perform AND(&) operation that will give you your desired output. Check below:

vulnerablePorts = ['21','22','110', etc]
systemPorts = ['22', '80']
comparedPorts = set(vulnerablePorts) & set(systemPorts)

This will give the new set containing the items present in both lists.

HNMN3
  • 542
  • 3
  • 9
3

Let's break it down step by step:

  1. Iterate over each item in systemPorts
  2. Check if the item is in vulnerablePorts, and if so, append it to comparedPorts

You could write this as a list comprehension (or a for loop, but it'd be less elegant).

Edit: Using a set is a different process, but it's just as good. It wouldn't preserve order, but the port numbers are ordered themselves, so you could just sort them before use if needed.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

You can try using the below code -

comparedPorts = [value for value in vulnerablePorts if value in systemPorts] 

Anyways the above approach will not perform well when it comes to time complexity and the given list are large in size.

In case you want the things to be bit faster use below code -

comparedPorts = list(set(vulnerablePorts) & set(systemPorts))
Ashutosh
  • 917
  • 10
  • 19
0

There are much more clever solutions but you could stick to a simple iterative search

vulnerablePorts = ['21','22','110']
systemPorts = ['22', '80']

comparedPorts = []
for sPort in systemPorts:
  for vPort in vulnerablePorts:
    if sPort is vPort:
      comparedPorts.append(sPort)

print(compatedPorts) # Prints ['22']

Interesting but by some crude testing this takes ~1.6ms per run compared to around ~2.2ms of some of the set operation solutions

Ben
  • 3,160
  • 3
  • 17
  • 34
0

It is a basic intersection operation of two list

def intersection(vulnerablePorts, systemPorts): 
    cmpPorts = [port for port in systemPorts if port in vulnerablePorts] 
    return set(cmpPorts)
-1

comparedPorts.extend([item for item in systemPorts if item in vulnerablePorts])

Frank Walker
  • 73
  • 2
  • 8