从实时tcpdump捕获中提取唯一的IP [英] Extract unique IPs from live tcpdump capture

查看:468
本文介绍了从实时tcpdump捕获中提取唯一的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下命令从实时tcpdump捕获中输出IP

I am using the following command to output IPs from live tcpdump capture

sudo tcpdump -nn -q ip -l | awk '{print $3; fflush(stdout)}' >> ips.txt

我得到以下输出

192.168.0.100.50771
192.168.0.100.50770
192.168.0.100.50759

需要2件事:

  1. 仅提取IP,而不提取端口.
  2. 生成具有唯一IP(无重复)的文件,并在可能的情况下进行排序.

提前谢谢

推荐答案

要从tcpdump中提取唯一的IP,可以使用:

To extract unique IPs from tcpdump you can use:

awk '{ ip = gensub(/([0-9]+.[0-9]+.[0-9]+.[0-9]+).*/,"\\1","g",$3); if(!d[ip]) { print ip; d[ip]=1; fflush(stdout) } }' YOURFILE

因此,您可以使用以下命令来实时查看唯一的IP:

So your command to see unique IPs live would be:

sudo tcpdump -nn -q ip -l | awk '{ ip = gensub(/([0-9]+.[0-9]+.[0-9]+.[0-9]+)(.*)/,"\\1","g",$3); if(!d[ip]) { print ip; d[ip]=1; fflush(stdout) } }'

这将在每个IP出现时立即打印要输出的IP,因此无法对其进行排序.如果要对它们进行排序,可以将输出保存到文件中,然后使用sort工具:

This will print each IP to output as soon as they appear, so it cannot sort them. If you want to sort those, you can save the output to a file and then use sort tool:

sudo tcpdump -nn -q ip -l | awk '{ ip = gensub(/([0-9]+.[0-9]+.[0-9]+.[0-9]+)(.*)/,"\\1","g",$3); if(!d[ip]) { print ip; d[ip]=1; fflush(stdout) } }' > IPFILE
sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4  IPFILE

示例输出:

34.216.156.21
95.46.98.113
117.18.237.29
151.101.65.69
192.168.1.101
192.168.1.102
193.239.68.8
193.239.71.100
202.96.134.133

注意:确保您使用的是gawk.它不适用于mawk.

NOTE: make sure you are using gawk. It doesn't work with mawk.

这篇关于从实时tcpdump捕获中提取唯一的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆