4 ms·
That's awesome but would you mind explaining this a little bit more to those not very unix savvy? I think I get the first three parts: 1) Create a temp buffer
by thyrox 5y ago
That's awesome but would you mind explaining this a little bit more to those not very unix savvy? I think I get the first three parts:
1) Create a temp buffer file
2) Read the file constantly to STDOUT?
3) What does netcat httpbin.org 80 | netcat -lp 8080 do?
What would be a good use of this circular pipeline? Can you give me some example of what you use this for? Thanks
- dundarious 5y agoI'm not OP, but as they said, it's an HTTP proxy. Run it, then point your browser at http://localhost:8080 http://localhost:8080. The message is proxied over to http://httpbin.org http://httpbin.org (on port 80, the normal HTTP port). If you're in an overly restricted environment and have a legit need to proxy to a very limited set of hosts, you can ad-hoc make a proxy mapping from local port to a single specific remote host. Just change the 8080, destination host, and buffer file. And I mean this as encouragement, but you should read the synopsis from the GNU netcat manual. It's short and explains it well enough. netcat httpbin.org 80 is using connect mode, the -l one is using listen mode, and -p is --local-port (the port to listen on in listen mode).
- maxbond 5y agoOf course! A.) Some context: - Our goal here is to match up to two network connections, so that anything we receive on one is forwarded to the other. - netcat is a "networking swiss army knife", it's a tool for making network connections - HTTP and bash pipelines both happen to be "line oriented", meaning they operate one line at a time. Exploiting this happy coincedence is what makes this work - httpbin.org is a metanym for any website you need. So: echo "" > /tmp/buffer Initialize our buffer to an empty state tail -f /tmp/buffer Read each line in the buffer, one at a time, and feed them to the next command. Do this forever (-f). netcat httpbin.org 80 Connect to the website httpbin.org. Transmit each line from our buffer to the website. netcat -lp 8080 > /tmp/buffer Accept a connection. Transmit any data we receive from httpbin.org to this client; write any data we receive from the client to the buffer, so that it can be transmitted to httpbin.org. B.) I don't think there is one! I've never needed to do this again in the, gosh, seven intervening years. Normally bash pipelines operate like a bucket brigade, moving data unidirectionally through various processing steps, and usually dumping it into a file at the end. They aren't normally for creating long-lived programs that manage bidirectional flows of data. Which is why writing a web server in bash is a fun challenge.
- thyrox 5y agoThank you for the detailed explanation. Learned a thing or two too! :)