-2

I am trying to get packets from a website hosted locally on remote computer(Test purpose) using pyshark.

Here is my code:

import pyshark

def print_live_dns():
   capture = pyshark.LiveCapture("wlan0")
   for packet in capture:
      # print(packet)
      with open('packets.txt', 'a') as f:
         f.write(str(packet))
      if "DNS" in packet and not packet.dns.flags_response.int_value:
         print(packet.dns.qry_name)

if __name__ == "__main__":
    print_live_dns()

With this code I only get packets from the internet. which is not what I need. How do I achieve this? using either pyshark, scapy, nmap etc

Bruno
  • 655
  • 8
  • 18
  • what is your expected output? – Narendra Prasath Jun 21 '20 at 12:13
  • Please provide excepted output and what you have tried so far. – Daweo Jun 21 '20 at 12:15
  • 1
    Duplicates: https://stackoverflow.com/questions/49032032/finding-the-intersection-of-nested-lists-in-python, https://stackoverflow.com/questions/3852780/python-intersection-of-multiple-lists – RoadRunner Jun 21 '20 at 12:22
  • 1
    Does this answer your question? [Finding the intersection of nested lists in Python?](https://stackoverflow.com/questions/49032032/finding-the-intersection-of-nested-lists-in-python) – Tibebes. M Jun 21 '20 at 12:24

6 Answers6

6

You can use set intersection

>>> from functools import reduce
>>>
>>> my_list = [[2,3,5,6,7,8,9], [2,4,78,23,13,7], [3,2,5,98,23,1,34]]
>>> list(reduce(lambda x, y: set(x).intersection(set(y)), my_list))
[2]
>>>
kundan
  • 1,278
  • 14
  • 27
1
from functools import reduce

my_list = [[2,3,5,6,7,8,9], [2,4,78,23,13,56,7], [3,2,5,98,23,1,34]]

print(reduce(set.intersection, map(set, my_list)))
Paul M.
  • 10,481
  • 2
  • 9
  • 15
0

What you need is to generate a set from each sub-list and calculate their intersection (the set including just elements present in each set):

intersect = set(my_list[0]) & set(my_list[1]) & set(my_list[2])

Before printing the result we first convert it back to a list:

print( list(intersect) )

My example is tailored on your specific list, intersection operator & is used. My next step would have been the explanation of the general case (N sub-lists) with loops, but it has been perfectly covered by the other answers and it would be redundant.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
0

You can use sets

my_list = [[2,3,5,6,7,8,9], [2,4,78,23,13,56,7], [3,2,5,98,23,1,34]]
res = set(my_list[0])
for i in range(1, len(my_list)):
    res = res.intersection(set(my_list[i]))
print(list(res))

Output

[2]
Leo Arad
  • 4,452
  • 2
  • 6
  • 17
0
from functools import reduce
my_list = [[2,3,5,56,7,8,9], [2,4,78,23,13,56,7], [3,2,5,98,23,1,34]]

# use the first list converted to a set as result, use *rest for iteration
result, *rest = map(set, my_list)
for sub in rest:
    result = result.intersection(sub)

print(result)

Another way of doing it is applying the & operation to converted sets:

import operator
list(reduce(operator.and_, map(set, l)))
Alexander Kosik
  • 669
  • 3
  • 10
0

you can use sets for this

 my_list = [[2,3,5,6,7,8,9], [2,4,78,23,13,56,7], [3,2,5,98,23,1,34]]
 first_tuple_list = [set(lst) for lst in my_list]
 print(first_tuple_list[0] & first_tuple_list[1] & first_tuple_list[2])
NAGA RAJ S
  • 452
  • 4
  • 12