I am writing a program in C to anal开发者_高级运维yze packets that are captured by tcpdump. In my program I use popen
to open a pipe to a tcpdump process which captures the packets and dumps the raw data to stdout as shown below.
FILE *tcpdump = popen("tcpdump -s0 -w -", "r");
However, warnings and error messages from tcpdump go straight to stderr, which is displayed in the console. I would like to somehow hide these, so only the output from my program is shown.
One way i tried was to append 2>&1
to the tcpdump command, but then my program would have to differentiate between tcpdump warnings/errors and the raw packet data.
Is there an easy way to silence the stderr output from tcpdump?
redirect stderr to /dev/null:
FILE *tcpdump = popen("tcpdump -s0 -w - 2>/dev/null", "r");
2>&1 means to redirect stderr to stdout, 2>file means to redirect stderr to file (and redirecting to /dev/null essentially ignores the output)
Look for the redirection explanation in man bash
精彩评论