Network Your Shell Scripts with Netpipesby Robert Bernier
There's no advantage to a sysadmin programming his own utility when he can do the same job (though perhaps less efficiently) in a quarter of the time with a quick and dirty shell script.
bash scripts can't solve all problems. Take, for example,
making socket connections to send and receive data streams. Conventional wisdom
states that you'll have to use an existing service such as FTP, or
create your own using a programming language such as Perl or C. I don't like
these solutions: I don't like the idea of having a service that only one person
uses. I don't like using full-blown programming languages on a server just to
make a couple of simple socket connections. What I want is a command-line
utility that anybody can use to set up TCP connections without worrying about
root privileges. I want something that works with standard input and output, is
easy to use in a script, and has a permissions system to restrict who can use
netpipes is a suite of utilities for shell-script writers that
builds on the idea of conventional pipes to allow different processes to
communicate and share data using both TCP and Unix domain-based sockets across
the network! Not only does it duplicate the pipe's behavior, but it uses a
novel technique called Session Control Protocol (SCP) that provides a simple
mechanism for creating multiple, lightweight connections over a single TCP
session connection. You can have many datastreams at the same time instead of
This article will introduce you to a few of the
netpipes command line
utilities. Refer to the man pages for more information.
There are six command-line utilities in the
faucet: The server end of a BSD network pipe.
hose: The client end of a BSD network pipe.
encapsulate: Multiplexes several datastreams or channels over a single channel.
timelimit: Caps how long a spawned subprocess can exist.
sockdown: Selectively shuts down all or a portion of a socket connection.
getsockname, in Debian): Returns information about the socket connection.
netpipes from Debian's testing distribution is easy enough:
apt-get install netpipes
Netpipes is also available from the FreeBSD ports collection.
This utility runs the server end. This example invocation sets up a server connection on port 2000 and gives a simple "hello world."
~$ faucet 2000 --out echo hello world
--out switch means that the server will spit out the
hello world as soon as a connection is made. You can also
run this in the background as a daemon by adding the
~$ faucet 2000 --out --daemon echo hello world
We can test this out using
~$ telnet localhost 2000 Trying 127.0.0.1... Connected to wolf. Escape character is '^]'. hello world Connection closed by foreign host.
The server can also carry out more than one instruction:
~$ faucet 2000 --out sh -c "echo hello world; echo hi again; sockdown 1 1; ls / > /tmp/temp.txt"
This example echoes two statements to the client,
hi again. It then closes the output connection using
netpipe utility. The next instruction sends the
output to the server's own file system in the file temp.txt.
Note: It's a good idea to enclose multiple commands within the
sh -c to avoid confusion.
hose is the client utility. We can contact the first server example in the
~$ hose localhost 2000 --in cat hello world ~$
--in switch means that the client expects incoming
information from the server.
cat is of course the concatenate
utility that takes input and puts it out as standard output.
The client can also send a datastream to the server. The server echoes the
hello it's my turn.
server: ~$ faucet 2000 --in sh -c "cat" client: ~$ hose localhost 2000 --out sh -c "echo -e hello it\\047s my turn"
This table shows several examples of simple piping -- one-way communications between the client and server.
|Server (||Client (||result|
||hello world (comes out on the server's terminal console)|
||I am hit, Tue Mar 9 18:51:31 EST 2004 (comes out on the client's terminal console)|
||Creates a file called temp1.txt on the server with information provided by the client.|
||Creates a file called temp2.txt on the client with
information provided by the server. Notice the use of redirection in
Two-Way Server/Client Communications
Piping feeds the output of one process as input to another, but lacks the ability of running bidirectional communications. You can, however, create two-way data flow.
netpipes permits you to choose whether it's the client or server that sends,
or receives, data upon connection by using the
--out switches in a specific order on both the client and server
commands. For example, if the client sends data as soon as it connects to the
server, then the first switch, reading from left to right, is
--out. The server must complement this by using the
Pages: 1, 2