简体   繁体   中英

create list of protocols and src,dst IP in scapy using python

I want to create and access a 3-d list of values parsed from a pcap file such that each row contains 3 values: row 1 = [protocol][source IP][destination IP].

I'm using scapy and here's the code I've worked on so far. It's obv not working and spittin gout only single row result:

pkts=rdpcap("conf.pcap")

def parsePcap():
  IPList = []
  count=0
  for pkt in pkts:
        #print pkt.summary()
        if pkt.haslayer(IP):
                proto = pkt.getlayer(IP).proto
                x = pkt.getlayer(IP).src
                y = pkt.getlayer(IP).dst
                IPList[count].append((proto,x,y))
                count+=1
                return IPList[count]


parsePcap()

Maybe because you are returning the IPList from within the loop, so as soon as it appends a row , it returns it, without getting a chance to check for more rows, you may want to return the list only at the end of the function, outside the for loop.

Example -

def parsePcap():
  IPList = []
  count=0
  for pkt in pkts:
        #print pkt.summary()
        if pkt.haslayer(IP):
                proto = pkt.getlayer(IP).proto
                x = pkt.getlayer(IP).src
                y = pkt.getlayer(IP).dst
                IPList[count].append((proto,x,y))
                count+=1
  return IPList[count]

Also, your indentation seems off, but I am hoping that is just a copy-paste issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM