简体   繁体   English

使用 scapy 读取 Python 中的 a.pcap 文件时出现问题

[英]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.我正在尝试创建一个程序,我必须在其中读取pcap文件,然后计算与某些 IP 相关的数据包数量。 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.我不习惯在 Python 中编程,但我必须使用它,因为我在 Raspberry Pi 上使用它,并且取决于 output 我必须控制几个引脚。

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 NameError:名称“数据包”未定义

The error apears to be in this ( for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap): ) line.错误出现在此( for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap): )行中。

Someone knows how to solve it?有人知道如何解决吗?

Thnak you:)谢谢你:)

As Carcigenicate pointed out, that's a known bug.正如 Carcigenicate 指出的那样,这是一个已知的错误。 It's fixed in https://github.com/secdev/scapy/commit/ff644181d9bee35979a84671690d8cd1aa1971fa它已在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您可以同时使用开发版本(超过https://scapy.readthedocs.io/en/latest/installation.html#current-development-version

Uninstall previous version & Install Latest version from https://pypi.org/project/scapy/https://pypi.org/project/scapy/卸载以前的版本并安装最新版本

pip install scapy==2.5.0rc1

This should fix the error这应该修复错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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