首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Scapy和tcpreplay:为性能绕过临时文件

Scapy和tcpreplay:为性能绕过临时文件
EN

Stack Overflow用户
提问于 2013-11-04 00:50:08
回答 1查看 4.2K关注 0票数 4

Scapy有一个使用tcpreplay发送数据包的sendpfast函数。但是,这个函数首先创建一个临时的pcap,然后调用tcpreplay。这增加了太多的延迟。是否存在绕过它直接将数据发送到tcpreplay的问题。我知道tcpreplay可以读取STDIN中的数据。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-25 16:14:20

不确定是否可以避免临时文件,但仍然有一种方法:

代码语言:javascript
复制
#! /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))
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19760102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档