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

时间:2018-06-26 11:23:07

标签: 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

///输出-仅放置前两行以获取思想

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) 

1 个答案:

答案 0 :(得分:0)

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

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

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

 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端口列。

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

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

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

相关问题