linux - 如何仅从 TCPDUMP 文件中提取目标端口

标签 linux bash

我目前在 bash 中有一个脚本,它仅从主机 IP(我)中提取目标 IP 和端口 我需要在单独的文件中对目标端口进行排序。

我现在用于捕获的命令:

tcpdump -G 15 -W 1 -w myscript15s -i enp0s3 -nnvvS src 10.0.2.15 and dst portrange '1-65535'  

有人知道我可以使用什么命令将目标端口放入单独的文档中吗? 我发现你只能对 IP 进行排序,但也许我搜索得不够彻底:(

Packet capture script
Packet capture output

//捕获脚本

#!/bin/bash
clear
echo "Select your capture option: "
read capture
echo "You selected  $catpure"
echo
if [ $capture == "Option1" ];
then
echo
tcpdump -G 15 -W 1 -w myscript15s -i enp0s3 -nnvvS src 10.0.2.15 and dst portrange '1-65535'
tcpdump -ttttnnr myscript15s
cp myscript15s captura
elif [ $capture == "Option2" ]
then
echo
tcpdump -G 600 -W 1 -w myscript600s -i enp0s3 -nnvvS src 10.0.2.15 and dst portrange '1-65535'
else
echo "Incorect option .."
fi
echo
echo "The end"
echo

///输出 - 只放置前 2 行以获得 ideea

2018-06-26 15:42:21.261263 IP 10.0.2.15.54178 > 10.18.0.22.53:19272 [1au] A? detectportal.firefox.com.(53)
2018-06-26 15:42:21.261418 IP 10.0.2.15.51118 > 10.18.0.22.53:31437+ [1au] AAAA? detectportal.firefox.com.(53) 

最佳答案

实现此目的的一种方法是使用 tshark 在应用显示过滤器的同时读取捕获,根据需要进行排序,然后将输出写入文件:

 tshark -r your_capture_file.pcap -T fields -e udp.dstport -e tcp.dstport | sort | uniq > results.txt

如果您还希望在结果中包含协议(protocol)名称,您也可以将其添加到过滤器中:

 tshark -r your_capture_file.pcap -T fields -e _ws.col.Protocol -e udp.dstport -e tcp.dstport | sort | uniq > results.txt

请注意,使用上述方法将在单个命令中处理所有事情,但输出结果将包含用于 TCP 流量的空白 UDP 端口列和用于 UDP 流量的空白 TCP 端口列,这可能会造成问题。

为了避免这种情况,您可以简单地运行命令两次,每个协议(protocol)一次:

TCP

tshark -r your_capture_file.pcap -T fields -e tcp.dstport | sort | uniq > results.txt

UDP

tshark -r your_capture_file.pcap -T fields -e udp.dstport | sort | uniq >> results.txt

请注意,第二次运行应使用 >> 运算符而不是 > 运算符将数据附加到结果文件。

关于linux - 如何仅从 TCPDUMP 文件中提取目标端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51041702/

相关文章:

bash - 统计git项目中所有文件的所有修订

linux - 从 Arduino Yun 上的 USB 接口(interface)获取 MIDI 音符

linux - 如何使用 seq 填充小数位

bash - 我如何通过管道传输 stderr 而不是 stdout?

c++ - 如何仅从 linux SO 库中导出某些函数?

linux - 一个 bash 脚本,用于解压 linux 子目录中的所有文件

linux - 如何在Linux中删除1天前的文件

linux - 在命令行 (Ubuntu) 上运行 Ipython notebook 时出错

linux - 董事会支持计划的组成部分、要求和规定。基于 SPI 的设备

windows - 跨平台编程语言 2D/3D?