Search this site

Metadata

Articles

Projects

Presentations

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:
HELLO THERE

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.

#!/bin/ksh
[ $# -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.

0 responses to 'poor man's netcat.'

Showing last 0 comments... (Click here to view all comments)


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: