正如 Christopher Maynard 所解释的,使用 editcap 一次最多只能选择 512 个数据包。Wireshark 邮件列表上的这个线程有更多信息。
如果您不想更改editcap的来源,您可以批量选择数据包。以下脚本生成 10000 个随机数,然后按 512 个批次选择数据包。生成的 .pcap 文件最后合并为单个 .pcap 文件。
#!/bin/bash
large_number=150000
smaller_number=10000
selected_pkt_numbers=$(shuf -i 0-"$large_number" -n "$smaller_number")
for j in `seq 0 512 $smaller_number`; do
endrange=$((j+512))
if [ "$endrange" -gt "$smaller_number" ]; then
endrange=$smaller_number
fi
# Selects numbers $j to $endrange from the generated random numbers:
echo "$j - $endrange"
pkt_numbers=$(echo $selected_pkt_numbers | awk -v start="$j" -v end="$endrange" '{ out=""; for (i=start+1; i<=end; i++) out=out" "$i; print out}')
editcap -r $1 $2-$j.pcap $pkt_numbers
done
mergecap -w $2.pcap `ls $2-*.pcap`
要使用它:
$ ./pcap-random.sh input-file.pcap output-file
0 - 512
512 - 1024
[...]
9216 - 9728
9728 - 10000
$
$
$ capinfos output-file.pcap
File name: output-file.pcap
File type: Wireshark/... - pcapng
File encapsulation: Ethernet
File timestamp precision: microseconds (6)
Packet size limit: file hdr: (not set)
Packet size limit: inferred: 58 bytes
Number of packets: 10 k
[...]
与修改 editcap 的源代码相比,该脚本的执行时间会更长。我没量过多少。使用您提供的参数,执行大约需要 11 秒。