本文介绍了Scapy和tcpreplay:绕过临时文件以提高性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

Scapy具有sendpfast函数,该函数使用tcpreplay发送数据包.但是,此函数首先创建一个临时的pcap文件,然后在该文件上调用tcpreplay.这增加了太多的延迟.无论如何,有没有绕过它,并直接将数据发送到tcpreplay.我知道tcpreplay可以从STDIN读取数据.

Scapy has a sendpfast function that sends packets using tcpreplay. However, this function first creates a temporary pcap file and then calls tcpreplay on that. This adds too much delay. Is there anyway to bypass it and directly send data to tcpreplay. I know that tcpreplay can read data from STDIN.

上下文:我想每秒产生大量流量(具有不同的srcIP)并通过网络发送.一种选择是将带有时间戳的所有流量保存在一个巨大的pcap文件中,然后运行tcpreplay.另一种选择是每秒发送一次数据.

Context: I want to generate large traffic (with different srcIP) every second and send it through network. One option is to save all traffic with timestamps in a giant pcap file and run tcpreplay. Another option is to send data every second.

推荐答案

不知道是否避免使用临时文件就足够了,但是仍然可以采用以下方法:

Not sure whether or not avoiding temporary file will be enough, but still, here is a way:

#! /usr/bin/env python

from scapy.all import *

def pkt2pcap(p):
        sec = int(p.time)
        usec = int(round((p.time-sec)*1000000))
        s = str(p)
        caplen = len(s)
        return struct.pack("IIII", sec, usec, caplen, caplen) + s

# adapted from Scapy's sendpfast
def mysendpfast(x, pps=None, mbps=None, realtime=None, loop=0, file_cache=False, iface=None):
    """Send packets at layer 2 using tcpreplay for performance
    pps:  packets per second
    mpbs: MBits per second
    realtime: use packet's timestamp, bending time with realtime value
    loop: number of times to process the packet list
    file_cache: cache packets in RAM instead of reading from disk at each iteration
    iface: output interface """
    if iface is None:
        iface = conf.iface
    argv = [conf.prog.tcpreplay, "--intf1=%s" % iface ]
    if pps is not None:
        argv.append("--pps=%i" % pps)
    elif mbps is not None:
        argv.append("--mbps=%i" % mbps)
    elif realtime is not None:
        argv.append("--multiplier=%i" % realtime)
    else:
        argv.append("--topspeed")
    if loop:
        argv.append("--loop=%i" % loop)
        if file_cache:
            argv.append("--enable-file-cache")
    argv.append("-")
    try:
        f = subprocess.Popen(argv, stdin=subprocess.PIPE)
        # PCAP header
        p = x.next()
        f.stdin.write(struct.pack("IHHIIII", 0xa1b2c3d4L,
                                  2, 4, 0, 0, MTU,
                                  conf.l2types[p.__class__]))
        # Let's send
        f.stdin.write(pkt2pcap(p))
        for p in x:
            f.stdin.write(pkt2pcap(p))
        f.stdin.close()
        f.communicate()
    except KeyboardInterrupt:
        log_interactive.info("Interrupted by user")
    except Exception,e:
        log_interactive.error("while trying to exec [%s]: %s" % (argv[0],e))

这篇关于Scapy和tcpreplay:绕过临时文件以提高性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 15:57