|

Linux inetd and shell input redirection

I was required to write a small listener. This listener doesn’t have to do anything fancy – it has to process input from a network source (over TCP) and shove it into a file with random name.

The quick and dirty solution is to use netcat (nc). A short command such as this would do the trick easily:

nc -l -p 10025 > /tmp/file.$$

This, however, will not allow multiple sessions, and, netcat tends to close when the session ends, which will require me to hack some weird workaround. Not the best method.

Well, then, to inetd we aim. I have decided to aim at inetd and not the more modern xinetd, since the previous is somewhat quicker to setup, and above all, it doesn’t really matter. You are most invited to transform the line supplied here to any other supersever you like 🙂

The script is located inside /usr/local/bin and it is called tcp_script . It is a very simple script:

#!/bin/bash
dd status=noxfer of=/tmp/out.$$

I have used ‘dd‘ because it worked well. I am sure other solutions are available as well.

inetd will redirect the standard input to the script, which will redirect it, in turn, to the file /tmp/out.$$

10025 stream tcp nowait root /usr/local/bin/tcp_script tcp_script

Reload (HUP, whatever) inetd, and you’re set.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.