1

I want to grep a multiline pattern from tcpdump output like the following: sudo tcpdump -A -s0 | grep -Pzo 'foo.*\n.*bar' However, it does not seem to work. But it works if I dump the data into a file and then grep the file. How can I make the command using pipe working?

Weihong
  • 11
  • 2
  • 3

3 Answers3

1

Try to add -l:

-l     Make stdout line buffered.  Useful if you want to see the data while capturing it.
       E.g.,
            tcpdump -l | tee dat
            tcpdump -l > dat & tail -f dat
konsolebox
  • 72,135
  • 12
  • 99
  • 105
0

I still don't get why the grep does not work above even with -l option for tcpdump, but I found this stackoverflow post How to find patterns across multiple lines using grep?. So I tried pcregrep, and it worked. sudo tcpdump -A -s0 | pcregrep -Mo "foo.*\n.*bar"

Community
  • 1
  • 1
Weihong
  • 11
  • 2
  • 3
0

I was having problems piping the output to tail even with the -l switch as well. I was able to solve my problem by using multitail instead of tail -F.

This worked for me: multitail -l "tcpdump -li eth0"

Ron DeFulio
  • 125
  • 1
  • 7