Search this site





poor man's netcat.

This script requires ksh. It makes use of a feature of ksh (zsh supports this too, but differently) called a co-process. A co-process is started by using the |& pipe at the end of a command. The file descriptor you use to write to is >&p. To read from it, you use <&p. Here's an example:
tr 'a-z' 'A-Z' |&
echo "hello there" >&p
exec 3>&p; exec 3>&-
cat <&p

The output is:

WTF?! You might be confused, I was when I first started playing with these. What happens is it runs tr 'a-z' 'A-Z' in the background as a co-process. Then it echos "hello there" to the input of tr. To signify that we are in fact done sending input, we have to close the input file descriptor, this is done through 2 statements: exec 3>&p - this opens file descriptor 3 and has it output to the co-process. The next statement, exec 3>&-, tells ksh you want to close file descriptor 3, which in turn closes the input to the co-process. The last line should be fairly obvious: cat <&p - it sends the output of the co-process through cat.
Ok, so you want that script do ya? Look below.

[ $# -ne 2 ] && echo "Invalid parameters" && exit 1
telnet $1 $2 |&
cat <&0 >&p
sed -e '1,3d' <&p

I won't get into much detail as to how things work here, should be pretty straight-forward. It takes 2 parameters, first being the host and second being the port. It reads from standard input and outputs to standard output.