Answers:
我希望有人仍然对解决问题感兴趣。;)我们公司遇到同样的问题,因此我开始为此编写脚本。
我也在下方分享了...
和代码:(请务必检查我的网站以获取将来的更新)
#!/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
我也不知道有任何答案。我找不到任何选择,无法回忆起曾经看到的任何东西,并且可以肯定的是tcpdump格式不包含接口标识符。我认为您必须为每个接口启动一个tcpdump实例并登录到相应的文件。
如果您在Mac上运行,则可以-k
选择tcpdump
是否使用pktap接口,该接口会在其他有用的元数据中转储接口名称。
-k Control the display of packet metadata via an optional metadata_arg argument. This is useful when displaying packet saved in the
pcap-ng file format or with interfaces that support the PKTAP data link type.
By default, when the metadata_arg optional argument is not specified, any available packet metadata information is printed out.
The metadata_arg argument controls the display of specific packet metadata information using a flag word, where each character
corresponds to a type of packet metadata as follows:
I interface name (or interface ID)
N process name
P process ID
S service class
D direction
C comment
U process UUID (not shown by default)
A display all types of metadata
This is an Apple modification.
添加到塞巴斯蒂安·哈斯的精彩bash脚本中。我不得不简化他的脚本,因为它在这一行中失败了tcpdump -l $@ | sed 's/^/[Interface:'"${BASH_REMATCH[0]:2}"'] /' &
。
尽管它不像原始脚本那样灵活,但它更有可能在精简的Linux系统中运行。
#!/bin/sh
interfaces="eth0 ip6tnl1" # Interfaces list separated by whitespace
#===================================================================================
#
# FILE: dump-stripped.sh
# USAGE: dump.sh [tcpdump-parameters]
# DESCRIPTION: tcpdump on any interface and add the prefix [Interace:xy] in
# front of the dump data. Simplified to work in more limited env.
# OPTIONS: similar to tcpdump
# REQUIREMENTS: tcpdump, sed, ifconfig, kill, awk, grep, posix regex matching
# AUTHOR: Sebastian Haas (Stripped down By Brian Khuu)
#
#===================================================================================
# 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:
for interface in $interfaces;
do tcpdump -l -i $interface -nn $@ | sed 's/^/[Interface:'"$interface"'] /' 2>/dev/null & done;
# wait .. until CTRL+C
wait;
您可能也对https://github.com/the-tcpdump-group/tcpdump/issues/296中有关此功能遗漏的当前github问题票感兴趣。
-e
仅使用在每一行上打印一个MAC地址。对于传入的数据包来说,它是源MAC,它对于确定到达哪个接口不是很有用。