
我正在嘗試捕獲 mysql 流量並將這些流量傳遞給 strings 命令,如下所示:
tcpdump -i any -s 0 -l -w - dst port 3306 | strings
這按預期工作並列印所有 mysql 查詢,例如
select * from mytables
show databases
但是當我嘗試將輸出重定向到文件時,它不會將輸出列印到/tmp/out
文件:
tcpdump -i any -s 0 -l -w - dst port 3306 | strings > /tmp/out
有人可以解釋上述命令的行為以及為什麼它不將輸出重定向到檔案。
答案1
我得到了解決方案:
實際上strings指令是緩衝的。我通過使用禁用了緩衝
stdbuf -i0 -o0 -e0 command
因此,將整個命令更改為以下內容後,輸出開始進入 /tmp/final 檔案。
tcpdump -i any -s 0 -l -w - dst port 3306 | stdbuf -i0 -o0 -e0 strings > /tmp/final