ZSHTCPSYS(1) ZSHTCPSYS(1)
NAME
zshtcpsys - zsh tcp system
DESCRIPTION
A module zsh/net/tcp is provided to provide network I/O over TCP/IP
from within the shell; see its description in zshmodules(1) . This
manual page describes a function suite based on the module. If the
module is installed, the functions are usually installed at the same
time, in which case they will be available for autoloading in the
default function search path. In addition to the zsh/net/tcp module,
the zsh/zselect module is used to implement timeouts on read opera-
tions. For troubleshooting tips, consult the corresponding advice for
the zftp functions described in zshftpsys(1) .
There are functions corresponding to the basic I/O operations open,
close, read and send, named tcpopen etc., as well as a function
tcpexpect for pattern match analysis of data read as input. The sys-
tem makes it easy to receive data from and send data to multiple named
sessions at once. In addition, it can be linked with the shell's line
editor in such a way that input data is automatically shown at the ter-
minal. Other facilities available including logging, filtering and
configurable output prompts.
To use the system where it is available, it should be enough to
`autoload -U tcpopen' and run tcpopen as documented below to start a
session. The tcpopen function will autoload the remaining functions.
TCP USER FUNCTIONS
Basic I/O
tcpopen [-qz] host port [ sess ]
tcpopen [-qz] [ -s sess -l sess,,... ] ...
tcpopen [-qz] [-a fd -f fd ] [ sess ]
Open a new session. In the first and simplest form, open a TCP
connection to host host at port port; numeric and symbolic forms
are understood for both.
If sess is given, this becomes the name of the session which can
be used to refer to multiple different TCP connections. If sess
is not given, the function will invent a numeric name value
(note this is not the same as the file descriptor to which the
session is attached). It is recommended that session names not
include `funny' characters, where funny characters are not
well-defined but certainly do not include alphanumerics or
underscores, and certainly do include whitespace.
In the second case, one or more sessions to be opened are given
by name. A single session name is given after -s and a
comma-separated list after -l; both options may be repeated as
many times as necessary. The host and port are read from the
file .ztcpsessions in the same directory as the user's zsh ini-
tialisation files, i.e. usually the home directory, but $$ZDOTDIR
if that is set. The file consists of lines each giving a ses-
sion name and the corresponding host and port, in that order
(note the session name comes first, not last), separated by
whitespace.
The third form allows passive and fake TCP connections. If the
option -a is used, its argument is a file descriptor open for
listening for connections. No function front-end is provided to
open such a file descriptor, but a call to `ztcp -l port' will
create one with the file descriptor stored in the parameter
$$REPLY. The listening port can be closed with `ztcp -c fd'. A
call to `tcpopen -a fd' will block until a remote TCP connec-
tion is made to port on the local machine. At this point, a
session is created in the usual way and is largely indistin-
guishable from an active connection created with one of the
first two forms.
If the option -f is used, its argument is a file descriptor
which is used directly as if it were a TCP session. How well
the remainder of the TCP function system copes with this depends
on what actually underlies this file descriptor. A regular file
is likely to be unusable; a FIFO (pipe) of some sort will work
better, but note that it is not a good idea for two different
sessions to attempt to read from the same FIFO at once.
If the option -q is given with any of the three forms, tcpopen
will not print informational messages, although it will in any
case exit with an appropriate status.
If the line editor (zle) is in use, which is typically the case
if the shell is interactive, tcpopen installs a handler inside
zle which will check for new data at the same time as it checks
for keyboard input. This is convenient as the shell consumes no
CPU time while waiting; the test is performed by the operating
system. Giving the option -z to any of the forms of tcpopen
prevents the handler from being installed, so data must be read
explicitly. Note, however, this is not necessary for executing
complete sets of send and read commands from a function, as zle
is not active at this point. Generally speaking, the handler is
only active when the shell is waiting for input at a command
prompt or in the vared builtin. The option has no effect if zle
is not active; `[ -o zle]' will test for this.
The first session to be opened becomes the current session and
subsequent calls to tcpopen do not change it. The current ses-
sion is stored in the parameter $$TCPSES; see below for more
detail about the parameters used by the system.
tcpclose [-qn] [ -a -l sess,,... sess ... ]
Close the named sessions, or the current session if none is
given, or all open sessions if -a is given. The options -l and
-s are both handled for consistency with tcpopen, although the
latter is redundant.
If the session being closed is the current one, $$TCPSES is
unset, leaving no current session, even if there are other ses-
sions still open.
If the session was opened with tcpopen -f, the file descriptor
is closed so long as it is in the range 0 to 9 accessible
directly from the command line. If the option -n is given, no
attempt will be made to close file descriptors in this case.
The -n option is not used for genuine ztcp session; the file
descriptors are always closed with the session.
If the option -q is given, no informational messages will be
printed.
tcpread [-bdq] [ -t TO ] [ -T TO ]
[ -a -u fd ... -l sess,,... -s sess ...]
Perform a read operation on the current session, or on a list of
sessions if any are given with -u, -l or -s, or all open ses-
sions if the option -a is given. Any of the -u, -l or -s
options may be repeated or mixed together. The -u option speci-
fies a file descriptor directly (only those managed by this sys-
tem are useful), the other two specify sessions as described for
tcpopen above.
The function checks for new data available on all the sessions
listed. Unless the -b option is given, it will not block wait-
ing for new data. Any one line of data from any of the avail-
able sessions will be read, stored in the parameter $$TCPLINE,
and displayed to standard output unless $$TCPSILENT contains a
non-empty string. When printed to standard output the string
$$TCPROMPT will be shown at the start of the line; the default
form for this includes the name of the session being read. See
below for more information on these parameters. In this mode,
tcpread can be called repeatedly until it returns status 2
which indicates all pending input from all specified sessions
has been handled.
With the option -b, equivalent to an infinite timeout, the func-
tion will block until a line is available to read from one of
the specified sessions. However, only a single line is
returned.
The option -d indicates that all pending input should be
drained. In this case tcpread may process multiple lines in
the manner given above; only the last is stored in $$TCPLINE,
but the complete set is stored in the array $$tcplines. This is
cleared at the start of each call to tcpread.
The options -t and -T specify a timeout in seconds, which may be
a floating point number for increased accuracy. With -t the
timeout is applied before each line read. With -T, the timeout
applies to the overall operation, possibly including multiple
read operations if the option -d is present; without this
option, there is no distinction between -t and -T.
The function does not print informational messages, but if the
option -q is given, no error message is printed for a non-exis-
tent session.
A return value of 2 indicates a timeout or no data to read. Any
other non-zero return value indicates some error condition.
See tcplog for how to control where data is sent by tcpread.
tcpsend [-nq] [ -s sess -l sess,,... ] data ...
tcpsend [-nq] -a data ...
Send the supplied data strings to all the specified sessions in
turn. The underlying operation differs little from a `print -r'
to the session's file descriptor, although it attempts to pre-
vent the shell from dying owing to a SIGPIPE caused by an
attempt to write to a defunct session.
The option -n prevents tcpsend from putting a newline at the
end of the data strings.
The remaining options all behave as for tcpread.
The data arguments are not further processed once they have been
passed to tcpsend; they are simply passed down to print -r.
If the parameter $$TCPOUTPUT is a non-empty string and logging
is enabled then the data sent to each session will be echoed to
the log file(s) with $$TCPOUTPUT in front where appropriate,
much in the manner of $$TCPROMPT.
Session anagement
tcpalias [-q] alias==sess ...
tcpalias [-q] [ alias ] ...
tcpalias -d [-q] alias ...
This function is not particularly well tested.
The first form creates an alias for a session name; alias can
then be used to refer to the existing session sess. As many
aliases may be listed as required.
The second form lists any aliases specified, or all aliases if
none.
The third form deletes all the aliases listed. The underlying
sessions are not affected.
The option -q suppresses an inconsistently chosen subset of
error messages.
tcplog [-asc] [ -n -N ] [ logfile ]
With an argument logfile, all future input from tcpread will be
logged to the named file. Unless -a (append) is given, this
file will first be truncated or created empty. With no argu-
ments, show the current status of logging.
With the option -s, per-session logging is enabled. Input from
tcpread is output to the file logfile.sess. As the session is
automatically discriminated by the filename, the contents are
raw (no $$TCPROMPT). The option -a applies as above.
Per-session logging and logging of all data in one file are not
mutually exclusive.
The option -c closes all logging, both complete and per-session
logs.
The options -n and -N respectively turn off or restore output of
data read by tcpread to standard output; hence `tcplog -cn'
turns off all output by tcpread.
The function is purely a convenient front end to setting the
parameters $$TCPLOG, $$TCPLOGSES, $$TCPSILENT, which are
described below.
tcprename old new
Rename session old to session new. The old name becomes
invalid.
tcpsess [ sess [ command ... ] ]
With no arguments, list all the open sessions and associated
file descriptors. The current session is marked with a star.
For use in functions, direct access to the parameters
$$tcpbyname, $$tcpbyfd and $$TCPSES is probably more conve-
nient; see below.
With a sess argument, set the current session to sess. This is
equivalent to changing $$TCPSES directly.
With additional arguments, temporarily set the current session
while executing the string command .... The first argument is
re-evaluated so as to expand aliases etc., but the remaining
arguments are passed through as the appear to tcpsess. The
original session is restored when tcpsess exits.
Advanced I/O
tcpcommand send-options ... send-arguments ...
This is a convenient front-end to tcpsend. All arguments are
passed to tcpsend, then the function pauses waiting for data.
While data is arriving at least every $$TCPTIMEOUT (default 0.3)
seconds, data is handled and printed out according to the cur-
rent settings. Status 0 is always returned.
This is generally only useful for interactive use, to prevent
the display becoming fragmented by output returned from the con-
nection. Within a programme or function it is generally better
to handle reading data by a more explicit method.
tcpexpect [ -q ] [ -p var ] [ -t to -T TO]
[ -a -s sess ... -l sess,,... ] pattern ...
Wait for input matching any of the given patterns from any of
the specified sessions. Input is ignored until an input line
matches one of the given patterns; at this point status zero is
returned, the matching line is stored in $$TCPLINE, and the full
set of lines read during the call to tcpexpect is stored in the
array $$tcpexpectlines.
Sessions are specified in the same way as tcpread: the default
is to use the current session, otherwise the sessions specified
by -a, -s, or -l are used.
Each pattern is a standard zsh extended-globbing pattern; note
that it needs to be quoted to avoid it being expanded immedi-
ately by filename generation. It must match the full line, so
to match a substring there must be a `**' at the start and end.
The line matched against includes the $$TCPROMPT added by
tcpread. It is possible to include the globbing flags `##b' or
`##m' in the patterns to make backreferences available in the
parameters $$MATCH, $$match, etc., as described in the base zsh
documentation on pattern matching.
Unlike tcpread, the default behaviour of tcpexpect is to block
indefinitely until the required input is found. This can be
modified by specifying a timeout with -t or -T; these function
as in tcpread, specifying a per-read or overall timeout,
respectively, in seconds, as an integer or floating-point num-
ber. As tcpread, the function returns status 2 if a timeout
occurs.
The function returns as soon as any one of the patterns given
match. If the caller needs to know which of the patterns
matched, the option -p var can be used; on return, $$var is set
to the number of the pattern using ordinary zsh indexing, i.e.
the first is 1, and so on. Note the absence of a `$$' in front
of var. To avoid clashes, the parameter cannot begin with
`expect'.
The option -q is passed directly down to tcpread.
As all input is done via tcpread, all the usual rules about
output of lines read apply. One exception is that the parameter
$$tcplines will only reflect the line actually matched by
tcpexpect; use $$tcpexpectlines for the full set of lines read
during the function call.
tcpproxy
This is a simple-minded function to accept a TCP connection and
execute a command with I/O redirected to the connection.
Extreme caution should be taken as there is no security whatso-
ever and this can leave your computer open to the world. Ide-
ally, it should only be used behind a firewall.
The first argument is a TCP port on which the function will lis-
ten.
The remaining arguments give a command and its arguments to exe-
cute with standard input, standard output and standard error
redirected to the file descriptor on which the TCP session has
been accepted. If no command is given, a new zsh is started.
This gives everyone on your network direct access to your
account, which in many cases will be a bad thing.
The command is run in the background, so tcpproxy can then
accept new connections. It continues to accept new connections
until interrupted.
tcpspam [-ertv] [ -a -s sess -l sess,,... ] cmd ...
Execute `cmd ...' for each session in turn. Note this executes
the command and arguments; it does not send the command line as
data unless the -t (transmit) option is given.
The sessions may be selected explicitly with the standard -a, -s
or -l options, or may be chosen implicitly. If none of the
three options is given the rules are: first, if the array
$$tcpspamlist is set, this is taken as the list of sessions,
otherwise all sessions are taken. Second, any sessions given in
the array $$tcpnospamlist are removed from the list of ses-
sions.
Normally, any sessions added by the `-a' flag or when all ses-
sions are chosen implicitly are spammed in alphabetic order;
sessions given by the $$tcpspamlist array or on the command
line are spammed in the order given. The -r flag reverses the
order however it was arrived it.
The -v flag specifies that a $$TCPROMPT will be output before
each session. This is output after any modification to TCPSES
by the user-defined tcponspam function described below.
(Obviously that function is able to generate its own output.)
If the option -e is present, the line given as cmd ... is exe-
cuted using eval, otherwise it is executed without any further
processing.
tcptalk
This is a fairly simple-minded attempt to force input to the
line editor to go straight to the default TCPSESION.
An escape string, $$TCPTALKESCAPE, default `:', is used to
allow access to normal shell operation. If it is on its own at
the start of the line, or followed only by whitespace, the line
editor returns to normal operation. Otherwise, the string and
any following whitespace are skipped and the remainder of the
line executed as shell input without any change of the line edi-
tor's operating mode.
The current implementation is somewhat deficient in terms of use
of the command history. For this reason, many users will prefer
to use some form of alternative approach for sending data easily
to the current session. One simple approach is to alias some
special character (such as `%%') to `tcpcommand --'.
tcpwait
The sole argument is an integer or floating point number which
gives the seconds to delay. The shell will do nothing for that
period except wait for input on all TCP sessions by calling
tcpread -a. This is similar to the interactive behaviour at
the command prompt when zle handlers are installed.
``One-shot'' file transfer
tcppoint port
tcpshoot host port
This pair of functions provide a simple way to transfer a file
between two hosts within the shell. Note, however, that bulk
data transfer is currently done using cat. tcppoint reads any
data arriving at port and sends it to standard output; tcpshoot
connects to port on host and sends its standard input. Any
unused port may be used; the standard mechanism for picking a
port is to think of a random four-digit number above 1024 until
one works.
To transfer a file from host woodcock to host springes, on
springes:
tcppoint 80091 >>outputfile
and on woodcock:
tcpshoot springes 80091 < |