简体   繁体   中英

Problems reading a .pcap file in Python using scapy

I'm trying to create a program where I have to read a pcap file and then count the number of packets related to some IPs. I'm not used to program in Python but I have to use it because I'm using it on a Raspberry Pi and depending of the output I have to control several pins.

Right now I have this, but I have an error and I don´t know how to solve it.

from scapy.all import *

from scapy.utils import RawPcapReader
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, TCP

def read_pcap(name_pcap):
    print("Opening", name_pcap)
    
    client_1 = '192.168.4.4:48878'
    server = '10.0.0.2:80'
    
    (client_1_ip, client_1_port) = client_1.split(':')
    (server_ip, server_port) = server.split(':')
    
    counter = 0
    
    for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap):
        counter += 1
        
        ether_pkt = Ether(pkt_data)

       # Below here are functions to filter the data
                
    
read_pcap("captura.pcap")

And the error is this one:

NameError: name 'Packet' is not defined

The error apears to be in this ( for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap): ) line.

Someone knows how to solve it?

Thnak you:)

As Carcigenicate pointed out, that's a known bug. It's fixed in https://github.com/secdev/scapy/commit/ff644181d9bee35979a84671690d8cd1aa1971fa

You can use the development version (over https://scapy.readthedocs.io/en/latest/installation.html#current-development-version ) in the meantime

Uninstall previous version & Install Latest version from https://pypi.org/project/scapy/

pip install scapy==2.5.0rc1

This should fix the error

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