Dog*_*rld 15 networking linux tcpdump
我正在尝试解决一个问题,即我在设备上只有 tcpdump 可用。我想使用 tcpdump 过滤网络流量,并且只显示包含某些字符串的流量。
我执行以下操作:
tcpdump -nei eth0 -X | grep "something interesting"
Run Code Online (Sandbox Code Playgroud)
输出是带有 16 字节 pr 行的十六进制视图。我无法 grep 此数据,因为数据显示在多行上。
tcpdump 有没有办法将捕获的数据显示在一行上?这将使使用 grep 查找感兴趣的数据包成为可能。
Evg*_*sky 13
对于像您一样不能使用 的人ngrep,这里是如何使用awk使tcpdump数据包内容的输出变得可检测的方法。
首先是由 提供的一些示例输出tcpdump -x,以便提前展示任务:
$ tcpdump -xr dump.pcap 2>/dev/null
12:04:59.590664 IP 10.17.14.93.51009 > 239.194.1.9.51009: UDP, length 370
0x0000: 4500 018e 0000 4000 fa11 7625 0a11 0e5d
0x0010: efc2 0109 c741 c741 017a 6f28 1120 2020
0x0020: 3337 3030 3039 3031 3835 3635 3430 3130
...
Run Code Online (Sandbox Code Playgroud)
这是您可以将输出通过管道传输到的可复制粘贴awk脚本
awk '{ if (match($0, /^[0-9]/, _)) { printf (NR == 1 ? "%s " : "\n%s "), $0; fflush() } else { sub(/^\s+0x[0-9a-z]+:\s+/, " "); gsub(" ", ""); printf "%s", $0 } } END { print ""; fflush() }'
Run Code Online (Sandbox Code Playgroud)
为了获得以下grepable输出
12:04:59.590664 IP 10.17.14.93.51009 > 239.194.1.9.51009: UDP, length 370 4500018e00004000fa1176250a...
12:04:59.590798 IP 10.17.14.113.51011 > 239.194.1.11.51011: UDP, length 370 4500018e00004000fa11760f...
...
Run Code Online (Sandbox Code Playgroud)
以下是上述脚本的注释版本:
awk '{
# if this is a header line
if (match($0, /^[0-9]/, _)) {
# print the header, but:
# except for the first line,
# we need to insert a newline,
# as the preceding data lines
# have been stripped of theirs
# we also append a space to
# separate header info from the
# data that will get appended
printf (NR == 1 ? "%s " : "\n%s "), $0
# enforce line-buffering
fflush()
}
# otherwise it is a data line
else {
# remove the data address
sub(/^\s+0x[0-9a-z]+:\s+/, " ");
# remove all spaces
gsub(" ", "");
# print w/o newline
printf "%s", $0
}
}
END {
# print final newline, as
# the preceding data lines
# have been stripped of theirs
print ""
# enforce line-buffering
fflush()
}'
Run Code Online (Sandbox Code Playgroud)
从tcpdump联机帮助页:
-A Print each packet (minus its link level header) in ASCII. Handy
for capturing web pages.
Run Code Online (Sandbox Code Playgroud)
确保您还使用该-s 0选项来确保显示整个数据包。