服务公告

服务公告 > Linux命令 > 源码编译tcpreplay,及使用方法

源码编译tcpreplay,及使用方法

发布时间:2026-01-08 00:02

蓝易云:源码编译 tcpreplay 及使用方法(Linux)

tcpreplay 是一套面向网络测试与故障复现的 pcap 回放工具,可把抓包文件按“原始时间轴”或“指定速率”重新注入到网卡,用于设备验收、链路压测、规则回归、问题复现等场景;它通常工作在二层注入位置,主机 TCP/IP 协议栈往往“看不到”这些回放包,这点在做验证时非常关键。(Tcpreplay)

截至目前(以官方发布记录为准),较新的版本为 v4.5.2(变更记录标注 2025-08-26,且发布页标记为 Latest)。(GitHub)


一、源码编译:两条路线(发行包 vs Git 源码)

路线A:使用 发行版源码包(更稳)

发行包通常自带已生成的 configure,一般不需要跑 autogen。(Tcpreplay)

1)安装依赖(Debian/Ubuntu)

sudo apt update
sudo apt install -y build-essential libpcap-dev pkg-config xz-utils
  • build-essential:提供 gcc/make 等编译链路,属于“交付底座”。
  • libpcap-dev:必需,没有它 configure 大概率直接失败。
  • pkg-config:帮助 configure 正确定位库路径。
  • xz-utils:解压 .tar.xz

2)编译安装(假设你已把 tcpreplay-4.5.2.tar.xz 放到当前目录)

tar -xf tcpreplay-4.5.2.tar.xz
cd tcpreplay-4.5.2
./configure --enable-64bits
make -j"$(nproc)"
sudo make install
sudo ldconfig
  • ./configure:生成与当前系统匹配的 Makefile;--enable-64bits 用 64 位计数器,处理大 pcap/循环更稳。(Tcpreplay)
  • make -j"$(nproc)":按 CPU 核心并行编译,加速交付。
  • sudo make install:把二进制与 man 等安装到系统路径。
  • ldconfig:刷新动态库缓存,避免“装了但找不到库”的尴尬。

3)验收(建议作为上线前的“冒烟测试”)

tcpreplay --version
tcpreplay --listnics
  • --version:确认版本信息。

  • --listnics:列出可用网卡名,减少把 eth0/ensXXX 写错的低级风险。([manpages.ubuntu.com](https://manpages.ubuntu.com/manpages/trusty/en/man1/tcpreplay.1.html "Ubuntu Manpage:

       tcpreplay - Replay network traffic stored in pcap files
    "))
    

路线B:使用 Git 源码(适合需要最新提交/修补)

从 Git 拉取时通常需要先运行 ./autogen.sh 生成 configure;而发行包一般不必。(GitHub)

1)额外依赖(Debian/Ubuntu)

sudo apt install -y autoconf automake libtool
  • 这三件套是 autotools 工具链,用来生成 configure/Makefile.in 等。

2)编译安装(仓库地址按官方项目为准,这里不直接展开)

git clone --depth=1 <官方仓库地址>
cd tcpreplay
./autogen.sh
./configure --enable-64bits
make -j"$(nproc)"
sudo make install
  • ./autogen.sh:把源码仓的构建脚本“生产化”,生成 configure。(GitHub)

二、核心使用:tcpreplay 回放的关键姿势 🙂

1)最小可用命令(单网卡)

sudo tcpreplay --intf1=eth0 demo.pcap
  • sudo:回放属于原始包注入,通常需要更高权限。
  • --intf1:指定输出网卡。(Tcpreplay)
  • demo.pcap:输入必须是 pcap(3) 格式;若是 pcapng,建议先转换(见下)。(Tcpreplay)

2)速率控制:四选一,别“混搭”

sudo tcpreplay --intf1=eth0 --pps=50000 demo.pcap
  • --pps:按“包/秒”回放。([manpages.ubuntu.com](https://manpages.ubuntu.com/manpages/trusty/en/man1/tcpreplay.1.html "Ubuntu Manpage:

       tcpreplay - Replay network traffic stored in pcap files
    "))
    
sudo tcpreplay --intf1=eth0 --mbps=100 demo.pcap
  • --mbps:按 Mbps 控速。([manpages.ubuntu.com](https://manpages.ubuntu.com/manpages/trusty/en/man1/tcpreplay.1.html "Ubuntu Manpage:

       tcpreplay - Replay network traffic stored in pcap files
    "))
    
sudo tcpreplay --intf1=eth0 --multiplier=2.0 demo.pcap
  • --multiplier:按抓包速率的倍数回放(2.0=两倍速)。([manpages.ubuntu.com](https://manpages.ubuntu.com/manpages/trusty/en/man1/tcpreplay.1.html "Ubuntu Manpage:

       tcpreplay - Replay network traffic stored in pcap files
    "))
    
sudo tcpreplay --intf1=eth0 --topspeed demo.pcap
  • --topspeed:尽可能快(以硬件/内核为上限)。([manpages.ubuntu.com](https://manpages.ubuntu.com/manpages/trusty/en/man1/tcpreplay.1.html "Ubuntu Manpage:

       tcpreplay - Replay network traffic stored in pcap files
    "))
    

重要提醒:--pps / --mbps / --multiplier / --topspeed 这些速率选项互斥,别同时上阵,否则不是报错就是结果不可控。([manpages.ubuntu.com](https://manpages.ubuntu.com/manpages/trusty/en/man1/tcpreplay.1.html "Ubuntu Manpage:

   tcpreplay - Replay network traffic stored in pcap files
"))

高 PPS 更稳的小技巧

sudo tcpreplay --intf1=eth0 --pps=200000 --pps-multi=10 demo.pcap
  • --pps-multi=10:每个时间片发 10 个包,拉长 sleep 间隔,让定时更可实现;该参数必须与 --pps 同用。(Tcpreplay)

3)循环、统计、预加载:把“可观测性”做扎实

sudo tcpreplay --intf1=eth0 --loop=100 --stats=1 --preload-pcap demo.pcap