linux - 带有连续管道的 grep 不起作用

标签 linux bash grep pipe stdout

(可能是“tcpflow”的问题)

我写了一个监控http流量的脚本,我安装了tcpflow,然后grep

有效(您应该发出一个 http 请求,例如 curl www.163.com)

sudo tcpflow -p -c -i eth0 port 80 2>/dev/null | grep '^Host: '

它像这样输出(连续)

Host: config.getsync.com
Host: i.stack.imgur.com
Host: www.gravatar.com
Host: www.gravatar.com

但我不能继续使用管道

不起作用(无输出)

sudo tcpflow -p -c -i eth0 port 80 2>/dev/null | grep '^Host: ' | cut -b 7- 

不起作用(无输出)

sudo tcpflow -p -c -i eth0 port 80 2>/dev/null | grep '^Host: ' | grep H

当我用 cat foo.txt 替换 sudo tcpflow 时,它起作用了:

cat foo.txt | grep '^Host: ' | grep H

那么 pipe 或 grep 或 tcpflow 有什么问题?


更新:

这是我的最终脚本:https://github.com/zhengkai/config/blob/master/script/monitor_outgoing_http.sh

最佳答案

grep 连续流使用--line-buffered 选项:

sudo tcpflow -p -c -i eth0 port 80 2> /dev/null | grep --line-buffered '^Host'

--line-buffered

Use line buffering on output. This can cause a performance penalty.


关于缓冲输出的一些思考(也提到了stdbuf工具):

Pipes, how do data flow in a pipeline?

关于linux - 带有连续管道的 grep 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42158140/

相关文章:

linux - 自定义 grep 文件输出

linux - 在版本名称和版本号之间进行过滤

linux - 从 bash 脚本调用时,Makefile 删除参数后的空白值

linux - 如何从 Linux 内核空间中的 socket connect() 获取本地源端口?

ruby - 为什么我不能从 Ruby 中调用 `history`?

linux - fish、zsh、bash 可以导入同一个配置文件吗?

linux - 将 base64 svg 数据解码为 svg 文件

c - 确定进程的状态

git - 从 PS1 提示符运行 git update-index --refresh 安全吗?

linux - 使用 grep 从另一个 linux 脚本检测 linux bash 脚本中的注释行