4

I know questions like this have been asked plenty of times before, but I think this is subtley different.

I am attempting to write a flexible traffic generator in Python using scapy. Producing the packet is fine, but when it comes to sending traffic at a sufficiently fast rate (for my needs, somewhere in the range of 500-700 packets per second), I seem to have hit a wall at around 20-30 pps.

I believe that there may be some need for threading, or am I missing something easier?

4

2 回答 2

7

在我的系统上,与使用 send 发送 IP 数据包相比,使用 sendp 发送以太网帧的性能要好得多。

# this gives appox 500pps on my system
pe=Ether()/IP(dst="10.13.37.218")/ICMP()
sendp(pe, loop=True)

# this gives approx 100pps on my system
pi=IP(dst="10.13.37.218")/ICMP()
send(pi, loop=True)

但是手动在套接字上发送(预先创建的)数据包要快得多:

s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
s.bind(("eth0", 0))
pe=Ether()/IP(dst="10.13.37.218")/ICMP()
data = pe.build()
while True:
    s.send(data)

但是将 pe.build() 移动到循环中会大大降低速度,暗示实际的数据包构建需要时间。

于 2011-09-23T19:33:46.867 回答
0

FTR,虽然上面的答案是正确的,但也可以使用 Scapy 套接字在第 2 级实现:

from scapy.all import *
sock = conf.L2socket()
pe=Ether()/IP(dst="10.13.37.218")/ICMP()
data = pe.build()
while True:
    pe.send(data)

虽然如果循环发送数据包是您的目标:

send(Ether()/IP(dst="10.13.37.218")/ICMP(), loop=1)

会做 :-)

于 2019-07-13T12:54:00.043 回答