This command will persistently serve its output--all hits from today on an apache server--to a browser, via netcat:
while true; do grep `date +%d\\/%b` /var/log/apache/access.log | nc -q1 -l -p8080; done
This is super-cool, but I have one pet-peeve with the way it works: the grep is run, and its output sits dormant until someone asks for it. This has two implications:
- There's no way to make use of the request headers to change what's being served
- The first request of the day will always serve yesterday's results, since the grep output has been sitting there since the last request. Two immediate requests in a row are the only way to know for certain that you're getting updated output.
My question is: Is there a way around this? I am dimly aware of crafting backpipes with mknod
and so forth, but after some serious Googling and manpage-reading, I can't really figure out if there's a solution I'm not seeing due to lack of experience, or if I'm simply trying to do something that isn't feasible with the way unix pipes work.
Edit: I see in man nc
that the -e
switch can be used to "Execute the specified command, using data from the network for stdin, and sending stdout and stderr to the network." It goes on to explain that this only works if the GAPING_SECURITY_HOLE
(lol) compile time option was enabled, which makes sense. So now I'm wondering, is this hugely insecure behavior what I'm actually trying to accomplish, or is there a safe middle ground?
Maybe doing just #2 (above) would be safe, but #1 wouldn't. Not sure yet.
Edit #2: Maybe something to do with named pipes?