我使用wireshark GUI将IO图形统计信息保存为包含每秒位数的CSV文件。有没有办法用命令行tshark生成这个CSV文件?我可以在命令行上生成每秒字节数的统计信息,如下所示
tshark -nr test.pcap -q -z io,stat,1,BYTES
如何生成位/秒并将其保存为CSV文件?
感谢任何帮助。
答案 0 :(得分:1)
我不知道仅使用tshark的方法,但是您可以轻松地将tshark的输出解析为CSV文件:
tshark -nr tmp.pcap -q -z io,stat,1,BYTES | grep -P "\d+\s+<>\s+\d+\s*\|\s+\d+" | awk -F '[ |]+' '{print $2","($5*8)}'
说明
grep -P "\d+\s+<>\s+\d+\s*\|\s+\d+"
仅从tshark输出中选择具有实际数据的原始数据(即second <> second | transmitted bytes
)。awk -F '[ |]+' '{print $2","($5*8)}'
用[ |]+
作为分隔符将该数据分成5个块,并显示2个块(第二个开始间隔)和5个块(传输的字节),中间用逗号隔开。 / li>
答案 1 :(得分:1)
另一件事可能是很好的了解:
如果您将间隔从1秒更改为0.5秒,则必须在grep部分中通过在两个数字.
之间添加\.
来允许\d
。
否则,结果将是一个空* .csv文件。
grep -P "\d{1,2}\.{1}\d{1,2}\s+<>\s+\d{1,2}\.{1}\d{1,2}\s*\|\s+\d+"
答案 2 :(得分:0)
此线程中的答案为我提供了使用 tshark io stats 解决类似问题的关键,我想分享结果及其工作原理。在我的例子中,任务是转换多列 tshark io stat 记录,并在数据中包含潜在的小数点。此答案将多个数据列转换为 csv,添加基本标题,考虑字段中的小数和可变数量的空格。
tshark -r capture.pcapng -q -z io,stat,30,,FRAMES,BYTES,"FRAMES()ip.src == 10.10.10.10","BYTES()ip.src == 10.10.10.10","FRAMES()ip.dst == 10.10.10.10","BYTES()ip.dst == 10.10.10.10" \
| grep -P "\d+\.?\d*\s+<>\s+|Interval +\|" \
| tr -d " " | tr "|" "," | sed -E 's/<>/,/; s/(^,|,$)//g; s/Interval/Start,Stop/g' > somefile.csv
命令字符串有 3 个主要部分。
tshark 以 30 秒的间隔运行 -z io,stat
,使用各种过滤器计算帧和字节数。
tshark -r capture.pcapng -q -z io,stat,30,,FRAMES,BYTES,"FRAMES()ip.src == 10.10.10.10","BYTES()ip.src == 10.10.10.10","FRAMES()ip.dst == 10.10.10.10","BYTES()ip.dst == 10.10.10.10"
这是针对我的测试 pcap 文件运行时的输出:
=================================================================================================
| IO Statistics |
| |
| Duration: 179.179180 secs |
| Interval: 30 secs |
| |
| Col 1: Frames and bytes |
| 2: FRAMES |
| 3: BYTES |
| 4: FRAMES()ip.src == 10.10.10.10 |
| 5: BYTES()ip.src == 10.10.10.10 |
| 6: FRAMES()ip.dst == 10.10.10.10 |
| 7: BYTES()ip.dst == 10.10.10.10 |
|-----------------------------------------------------------------------------------------------|
| |1 |2 |3 |4 |5 |6 |7 |
| Interval | Frames | Bytes | FRAMES | BYTES | FRAMES | BYTES | FRAMES | BYTES |
|-----------------------------------------------------------------------------------------------|
| 0 <> 30 | 107813 | 120111352 | 107813 | 120111352 | 26682 | 15294257 | 80994 | 104808983 |
| 30 <> 60 | 122437 | 124508575 | 122437 | 124508575 | 49331 | 17080888 | 73017 | 107422509 |
| 60 <> 90 | 138999 | 135488315 | 138999 | 135488315 | 54829 | 22130920 | 84029 | 113348686 |
| 90 <> 120 | 158241 | 217781653 | 158241 | 217781653 | 42103 | 15870237 | 115971 | 201901201 |
| 120 <> 150 | 111708 | 131890800 | 111708 | 131890800 | 43709 | 18800647 | 67871 | 113082296 |
| 150 <> Dur | 123736 | 142639416 | 123736 | 142639416 | 50754 | 22053280 | 72786 | 120574520 |
=================================================================================================
看看这个输出,我们可以看到几个需要考虑的项目:
一旦 tshark 产生输出,我们使用 grep 和正则表达式来提取我们想要保存的行。
grep -P "\d+\.?\d*\s+<>\s+|Interval +\|""
grep 将使用 Interval 列中的“Digit(s)Space(s)<>Space(s)”字符序列来匹配行与数据。它还使用 OR 通过匹配字符“Interval |”来获取标题。
grep -P # The "-P" flag turns on PCRE regex matching, which is not the same as egrep. With egrep, you will need to change the escaping.
"\d+ # Match on 1 or more Digits. This is the 1st set of numbers in the Interval column.
\.? # 0 or 1 Periods. We need this to handle possible fractional seconds.
\d* # 0 or more Digits. To handle possible fractional seconds.
\s+<>\s+ # 1 or more Spaces followed by the Characters "<>", then 1 or more Spaces.
| # Since this is not escaped, it is a regex OR
Interval\s+\|" # Match the String "Interval" followed by 1 or more Spaces and a literal "|".
从 tshark 输出中,grep 匹配了以下几行:
| Interval | Frames | Bytes | FRAMES | BYTES | FRAMES | BYTES | FRAMES | BYTES |
| 0 <> 30 | 107813 | 120111352 | 107813 | 120111352 | 26682 | 15294257 | 80994 | 104808983 |
| 30 <> 60 | 122437 | 124508575 | 122437 | 124508575 | 49331 | 17080888 | 73017 | 107422509 |
| 60 <> 90 | 138999 | 135488315 | 138999 | 135488315 | 54829 | 22130920 | 84029 | 113348686 |
| 90 <> 120 | 158241 | 217781653 | 158241 | 217781653 | 42103 | 15870237 | 115971 | 201901201 |
| 120 <> 150 | 111708 | 131890800 | 111708 | 131890800 | 43709 | 18800647 | 67871 | 113082296 |
| 150 <> Dur | 123736 | 142639416 | 123736 | 142639416 | 50754 | 22053280 | 72786 | 120574520 |
tr 和 sed 用于将 grep 匹配的行转换为 csv。 tr 执行删除空格和更改“|”的大量工作到 ”,”。这比使用 sed 更简单、更快。但是,sed 用于一些清理工作
tr -d " " | tr "|" "," | sed -E 's/<>/,/; s/(^,|,$)//g; s/Interval/Start,Stop/g'
以下是这些命令如何执行转换。第一个技巧是摆脱所有的空间。这意味着我们不必在任何正则表达式序列中考虑它们,使其余的工作更简单
| tr -d " " # Spaces are in the way, so delete them.
| tr "|" "," # Change all "|" Characters to ",".
| sed -E 's/<>/,/; # Change "<>" to "," splitting the Interval column.
s/(^,|,$)//g; # Delete leading and/or trailing "," on each line.
s/Interval/Start,Stop/g' # Each of the "Interval" columns needs a header, so change the text "Interval" into two words with a , separating them.
> somefile.csv # Pipe the output into somefile.csv
完成此过程后,我们就有了一个 csv 输出,现在可以将其导入您最喜欢的 csv 工具、电子表格,或馈送到 gnuplot 等绘图程序。
$cat somefile.csv
Start,Stop,Frames,Bytes,FRAMES,BYTES,FRAMES,BYTES,FRAMES,BYTES
0,30,107813,120111352,107813,120111352,26682,15294257,80994,104808983
30,60,122437,124508575,122437,124508575,49331,17080888,73017,107422509
60,90,138999,135488315,138999,135488315,54829,22130920,84029,113348686
90,120,158241,217781653,158241,217781653,42103,15870237,115971,201901201
120,150,111708,131890800,111708,131890800,43709,18800647,67871,113082296
150,Dur,123736,142639416,123736,142639416,50754,22053280,72786,120574520