0

I'm new to python i have set for Pcap files in a directory. i should read each file and extract the required data based on differnt variable for each file. i'm using pyshark for parsing pcap.

i have to take csv file column as an input for each filtering file

enter image description here

for example i have 4 files each in src- and dst- so first file i should be filtered by taking 10.272.726.227 only and for 2nd file 10.272.726.228 etc...

see below,

files = os.listdir('./Pcap')
csv_file=pd.read_csv('input.csv')
ip_src = csv_file.SRC_privateIp.tolist()
ip_dst = csv_file.DST_privateIp.tolist()  

for file in files:
    if file.startswith('src-'):
        cap_src = pyshark.FileCapture(file, only_summaries = True)
        for packet in cap_src:
            line=str(packet)
            formattedline = line.split(' ')
            if formattedline[2] == ip_src and formattedline[3] == ip_dst:
                print(formattedline)

    if file.startswith('dst-'):
        cap_src = pyshark.FileCapture(file, only_summaries = True)
        for packet in cap_src:
            line=str(packet)
            formattedline = line.split(' ')
            if formattedline[2] == ip_dst and formattedline[3] == ip_src :
                print(formattedline)

I tried to open each file and do processing on each file separately but it is taking all files data in one string. I want each file to open one by one, do processing because each file has different conditions to filter out the necessary. The above code gives an error This event loop is already running. i dont know how to proceed further Can anybody help me out?

Thanks!

1 Answers1

0

I do not understand what your question is, but I think you can use pandas for csv reading, subsetting and manipulations. It is the standard library for this kind of tasks.

Read csv with pandas or

Subset a dataframe with pandas

DaveR
  • 1,696
  • 18
  • 24
  • i edited my question can please guide how to approach further @DeveR – Darshan Mahalinge Gowda Jun 15 '20 at 11:25
  • what do you meanwith 'takes all file in one string' ? Can you show what 1 or 2 file looks like (https://kite.com/python/answers/how-to-create-a-pandas-dataframe-from-a-string-in-python) and what is the expected output (just to have an idea)? Do you want to filter some rows or what? – DaveR Jun 16 '20 at 12:37