在 Linux shell (Bash) 中使用命名管道的示例 [英] Example of using named pipes in Linux shell (Bash)

查看:178
本文介绍了在 Linux shell (Bash) 中使用命名管道的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以发布一个在 Linux 上的 Bash 中使用命名管道的简单示例吗?

Can someone post a simple example of using named pipes in Bash on Linux?

推荐答案

实际使用命名管道的最佳示例之一...

One of the best examples of a practical use of a named pipe...

来自http://en.wikipedia.org/wiki/Netcat:

另一个有用的行为是使用 netcat 作为代理.端口和主机都可以重定向.看这个例子:

Another useful behavior is using netcat as a proxy. Both ports and hosts can be redirected. Look at this example:

nc -l 12345 | nc www.google.com 80

端口 12345 代表请求.

Port 12345 represents the request.

这会在端口 12345 上启动一个 nc 服务器,并且所有连接都被重定向到 google.com:80.如果 Web 浏览器向 nc 发出请求,该请求将发送到 google,但不会将响应发送到 Web 浏览器.那是因为管道是单向的.这可以通过命名管道来解决,以重定向输入和输出.

This starts a nc server on port 12345 and all the connections get redirected to google.com:80. If a web browser makes a request to nc, the request will be sent to google but the response will not be sent to the web browser. That is because pipes are unidirectional. This can be worked around with a named pipe to redirect the input and output.

mkfifo backpipe
nc -l 12345  0<backpipe | nc www.google.com 80 1>backpipe

这篇关于在 Linux shell (Bash) 中使用命名管道的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆