'How to control Scapy sending packets speed?

I am using Scapy to forge random tcp and udp flows to test packet-in flooding in my SDN experiment. But I don't know how to control Scapy sending packets speed. For example, if I want to set Scapy sending packets 1000 pkt/s, what should I do ?



Solution 1:[1]

The most efficient way would be to use scapy’s sendpfast together with tcpreplay (it must be installed):

>>> pkt = Ether()/IP()/TCP()/"blobofdata". # must be layer 2
# let’s send 10000 packets at 1000 packets per seconds
>>> sendpfast(pkt, pps=1000, loop=10000, parse_results=1)
{'packets': 10000,
 'bytes': 660000,
 'time': 9.99,
 'bps': 66006.1,
 'mbps': 0.528,
 'pps': 1000.09,
 'flows': 1,
 'fps': 0.1,
 'flow_packets': 100000000,
 'non_flow': 0,
 'successful': 10000,
 'failed': 0,
 'truncated': 0,
 'retried_enobufs': 0,
 'retried_eagain': 0,
 'command': 'tcpreplay --intf1=enp0s3 --pps=1000 --loop=10000 /tmp/scapyo6ilzjdk',
 'warnings': []}
>>>

You even get some stats. This allows to workaround Python limitations that wouldn’t allow such a high rate of packets.

If you are aiming at lower rates, you could use inter from send

>>> sendp(pkt, count=10000, inter=1./20)  # 20 packets per second

Solution 2:[2]

I spent a lot of time to understand and find how to send packet in other then eth0 interface. My task was - create UDP Flood. In sendp i could have random udp ports, but with low speed, in this case with sendpfast you can have speed, if you have a good CPU, but only repeat the same packet and this 'Fuzz/RandString' dosent work. 'Parse_reuslts' dosent worked in my case.

conf.iface='eth1'
pkt = Ether()/IP(dst="20.20.20.2")/fuzz(UDP())/Raw(RandString(size=1400))
sendpfast(pkt, mbps=1000, loop=50000)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 G A R M