如何在 tcpdump 输出流中显示界面?

mdr*_*iel 25 linux packet-sniffer tcpdump

这似乎是一个微不足道的问题,但经过一番搜索后,我仍然想不出答案。可以使用“any”作为接口描述来运行 tcpdump,即:

 # tcpdump -i any -n host 192.168.0.1
Run Code Online (Sandbox Code Playgroud)

有什么方法可以强制 tcpdump 显示在哪个接口上显示的数据包被捕获?

更新:

随着越来越多的人确认使用 vanilla tcpdump 可能无法做到这一点,有人可以提出解决上述问题的方法吗?也许不同的嗅探器?

一般问题如下:在具有 50 个接口的系统上,确定来自特定 IP 地址的数据包的入站接口是什么。

小智 24

我希望有人仍然对问题的解决方案感兴趣。;) 我们公司也遇到了同样的问题,我开始为此编写脚本。

我用源代码和屏幕截图写了一篇关于它的博客文章

我也在下面分享了它......

在此处输入图片说明

和代码:(一定要检查我的网站以获取未来的更新)

#!/bin/bash
#===================================================================================
#
# FILE: dump.sh
# USAGE: dump.sh [-i interface] [tcpdump-parameters]
# DESCRIPTION: tcpdump on any interface and add the prefix [Interace:xy] in front of the dump data.
# OPTIONS: same as tcpdump
# REQUIREMENTS: tcpdump, sed, ifconfig, kill, awk, grep, posix regex matching
# BUGS:  ---
# FIXED: - In 1.0 The parameter -w would not work without -i parameter as multiple tcpdumps are started.
#        - In 1.1 VLAN's would not be shown if a single interface was dumped.
# NOTES: ---
#        - 1.2 git initial
# AUTHOR: Sebastian Haas
# COMPANY: pharma mall
# VERSION: 1.2
# CREATED: 16.09.2014
# REVISION: 22.09.2014
#
#===================================================================================

# When this exits, exit all background processes:
trap 'kill $(jobs -p) &> /dev/null && sleep 0.2 &&  echo ' EXIT
# Create one tcpdump output per interface and add an identifier to the beginning of each line:
if [[ $@ =~ -i[[:space:]]?[^[:space:]]+ ]]; then
    tcpdump -l $@ | sed 's/^/[Interface:'"${BASH_REMATCH[0]:2}"'] /' &
else
    for interface in $(ifconfig | grep '^[a-z0-9]' | awk '{print $1}')
    do
       tcpdump -l -i $interface -nn $@ | sed 's/^/[Interface:'"$interface"']    /' &
    done
fi
# wait .. until CTRL+C
wait
Run Code Online (Sandbox Code Playgroud)


小智 11

请注意,tcpdump 4.99 现在在输出中显示接口名称/方向:

[vagrant@localhost]$sudo tcpdump -i any arp
tcpdump: data link type LINUX_SLL2
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
18:38:49.662151 br-ex Out ARP, Request who-has 172.18.1.10 tell localhost.localdomain, length 28
18:38:49.662432 br-ex In  ARP, Reply 172.18.1.10 is-at fa:16:3e:31:b9:10 (oui Unknown), length 28
Run Code Online (Sandbox Code Playgroud)

接口 br-ex,上面。


小智 10

您可以使用 -e 选项打印以太网标头,然后您可以将 src/dst MAC 地址与您的网络接口相关联;)。

  • 使用“-e”仅在每一行打印一个 MAC 地址。对于传入数据包,它是源 MAC,对于识别数据包到达哪个接口来说并不是很有用。 (2认同)

Jef*_*and 0

我也不知道对此有何答案。我找不到它的选项,不记得曾经见过,并且相当确定 tcpdump 格式不包含接口标识符。我认为您必须为每个接口启动一个 tcpdump 实例并记录到相应的文件。

  • 我确实经常需要这个功能。我有几个接口,很多 vlan 接口,上面还有 IGP 和 BGP。了解数据包如何流动通常非常重要。我可以通过检查当前路由表来手动检查出站接口。但是,如果我必须找到数据包来自互联网的方式,有时我必须进行盲目检查,只需在最可能的接口上启动 tcpdump 即可。:( (2认同)