TCPDUMP出力をリアルタイムで処理/パイプライン化する方法

TCPDUMP出力をリアルタイムで処理/パイプライン化する方法

クライアント(OpenWrt 10.04ルーター上)を介してDNS要求をtcpdumpするには、次の手順を実行します。

root@ROUTER:/etc# tcpdump -n -i br-lan dst port 53 2>&1       
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on br-lan, link-type EN10MB (Ethernet), capture size 96 bytes
22:29:38.989412 IP 192.168.1.200.55919 > 192.168.1.1.53: 5697+ A? foo.org. (25)
22:29:39.538981 IP 192.168.1.200.60071 > 192.168.1.1.53: 17481+ PTR? 150.33.87.208.in-addr.arpa. (44)
^C
2 packets captured
3 packets received by filter
0 packets dropped by kernel

完全に大丈夫です。しかし。 tcpdumps出力をリアルタイムでストリーミングできないのはなぜですか?

root@ROUTER:/etc# tcpdump -n -i br-lan dst port 53 2>&1 | awk '/\?/ {print $3}'
^C
root@ROUTER:/etc# 

tcpdumpの後にawkのような操作を実行すると、何の出力も得られません。なぜそんなことですか? tcpdumpの出力をリアルタイムでパイプできないのはなぜですか? (例では、3番目の列のみが出力されます。)

解決策はありますか?

ベストアンサー1

すぐに出るman tcpdump

-l     Make stdout line buffered.  Useful if you want to see the data while 
       capturing it.  E.g.,

              tcpdump -l | tee dat

       or

              tcpdump -l > dat & tail -f dat

       Note that on Windows,``line buffered'' means ``unbuffered'', so that 
       WinDump will write each character individually if -l is specified.

       -U is similar to -l in its behavior, but it will cause output to be 
       ``packet-buffered'', so that the output is written to stdout at the 
       end of each packet rather than at the end of each line; this is 
       buffered on all platforms, including Windows.

おすすめ記事