linux环境下批量回放报文
安装工具
sudo apt install tcpreplay
编写脚本
root@wuhu:/vicata
import os
import subprocess
def replay_pcaps(directory, interface):
pcap_files = [f for f in os.listdir(directory) if f.endswith('.pcap')]
while True:
for pcap_file in pcap_files:
pcap_path = os.path.join(directory, pcap_file)
print("Replaying {} on interface {}".format(pcap_path, interface))
command = ['/usr/bin/tcpreplay', '-l 5', '--mbps=100', '-i', interface, pcap_path]
try:
result = subprocess.call(command)
print("Successfully replayed {}".format(pcap_path))
except subprocess.CalledProcessError as e:
print("Failed to replay {}: {}".format(pcap_path, e))
except Exception as e:
print("An unexpected error occurred while replaying {}: {}".format(pcap_path, e))
time.sleep(5)
if __name__ == "__main__":
pcap_directory = '/your/pcaps/path'
network_interface = 'ens33'
replay_pcaps(pcap_directory, network_interface)
参数说明
/usr/bin/tcpreplay -l 5 --mbps=100 -i ens33 a.pcap
-l 5: 这个选项指定了循环播放的次数。在这个例子中,-l 5 表示将循环播放 5 次。如果省略这个选项,tcpreplay 将只播放一次循环播放设置为0即可。
--mbps=100: 这个选项设置重放的速度为 100 Mbps。
-i ens33: 这个选项指定了要使用的网络接口。
运行脚本
root@wuhu:/vicata