|
NAMErex - remote execution utilitySYNOPSISrex [-NVdlnvh] [-c FILE] [--config=FILE] [--noop] [--no-init-file] [--no-rc-file] [--no-resolve] [--verbose] [--version] [--help] COMMAND [ARG...]DESCRIPTIONRex runs a supplied command or shell script on several hosts in succession or in parallel. It can also be used to copy a file or files to several hosts.The two main COMMANDs are run, which runs the specified command on multiple hosts, and copy, or cp, which copies files to them. For both commands, there are two ways to select hosts to operate upon. The first way is the -H (--host) option. Its argument is either a single hostname or IP, or a comma-separated list of hostnames or IP addresses. The option adds these hosts to the list. Multiple options can be specified. The -H option exists mainly as a way of quickly running a job of several hosts at once. If you have a set of hosts which you often work with, it's best to use a hostgroup. Hostgroup defines a list of hosts and, optionally, a set of rex settings to use when running commands on them. Each hostgroup is identified by its name and is defined in a separate disk file. See the subsection Hostgroups, for a detailed discussion of how to define a hostgroup. Once defined, the option -g (--group) can be used to run a command on each host in the group. The argument to the option is the name of the group. Thus, e.g. rex run -gNAME COMMAND runs COMMAND on hosts from hostgroup NAME, and rex copy -gNAME FILE /tmp copies FILE to the directory /tmp on each host from this hostgroup. If the -g option is omitted, rex will use the hostgroup from the top of the hostgroup stack or, if it is empty, the default hostgroup. Hostgroup stack is maintained using the rex group command. Once the user pushes a hostgroup name on it, that hostgroup will be used by any subsequent rex command, unless it is given the -g option. The rex group command provides ways to pop items off the stack, swap arbitrary element and the top of the stack, select new hostgroups on the fly, etc. See the section REX GROUP below for a detailed discussion of hostgroup stack. The default hostgroup is used if the hostgroup stack is empty. It is defined using the option group statement in the configuration file. The list of hosts in a hostgroup can be modified for the duration of a single rex command by using the -H and -X options. The -H (--host) option works exactly as described above. The -i (--ignore-hosts) option can be given to ignore the list of hosts defined in the hostgroup and operate only on those supplied with the --host options. The -X (--exclude) option instructs rex to remove its argument from the list of hosts. Its argument is the domain name or IP address of the host. It need not be exactly the same name or IP as the one used in the set hosts statement in the rc file. It suffices that it resolves to the same IP as one of the hosts listed in the hostgroup. The list of all available hostgroups can be obtained using the list command. The list is organized in two columns: group name and its short description (if available). Login credentialsNormally, remote hosts should be configured to accept logins to the user account without password, by using shared keys. However, it is also possible to work on hosts requiring password for authentication. To do so, rex keeps the database of login credentials. The database is edited using the commandrex edit This command starts the editor defined by the VISUAL (or, if unset, EDITOR) environment variable. Upon exit from the editor, it compares the new content with the existing one. If changed, the user is prompted whether they wish to save the changes. The database is a dictionary of key-value pairs. A key identifies the credential (e.g. user, for username, or pass, for password). Unless prefixed with one or more qualifiers, it is valid for all servers in all hostgroups. Qualifiers limit the scope of a key to a given hostgroup or server. In general, the syntax for a key is: [[GROUP:][HOST]:]KEYWORD When looking for a key, the most qualified match is preferred, so the lookup order is:
For example, to determine the login name for the given host, rex looks up for the keyword user. If no matching key is found and the the -u (--user) option is given, its value is used. Otherwise, the name of the value of the environment variable USER is used. Similarly, the password to use is determined by the following look-ups:
If none of these keys is found, the value of the -p (--password) option is used. If this option is not supplied either, passwordless account is assumed. Configuration filesUpon startup rex looks for the following files and attempts to source them:
These files are not required to exist. If they do the user settings override the system-wide ones. These files are sourced as regular TCL code. Normally, they modify the rex settings by using a st of commands provided for that purpose:
ifmode { run { environ TERM=dump } }
option jobs 25 has the same effect as giving the option --jobs=25 in the command line. Options that take no argument in the command line are treated as booleans. The VALUE of on, true, yes, or 1 sets the option, while the VALUE of off, false, no, or 0 unsets it. Thus, to set the no-resolve option, use option no-resolve true
HostgroupsA hostgroup defines a list of hosts and optionally associates a set of rex settings to use when running commands on them. Each hostgroup is identified by its name, which must comply with the same requirements as a name of a disk file. The hostgroup definition consists of one or more files located in the directory ~/.rex/hostgroup/NAME (user-specific), or /usr/local/etc/rex/hostgroup/NAME (system-wide), where NAME is the name of the group. User-specific directories are given preference over the system-wide ones. The only mandatory file is rc. It is a TCL source, which will be loaded in a separate namespace, to avoid the possibility of inadvertent contamination of the main rex code.The hostgroup rc file should use the host and/or hostgroup commands (see the subsection Configuration files above) to define the list of hosts in the hostgroup. DebuggingTo see what's going on during rex invocation, use the -d (--debug) option. This option is incremental, that is the more times you repeat it in the command line, the more debugging information is output. Currently, the most detailed information is obtained at level 3, i.e. using -ddd.Additional information is provided by the -l (--log) option, which enables logging of the send/expect dialog to stdout. If you wish to get even more information and are familiar with expect(1) internals, set the EXPECT_DEBUG environment variable prior to invoking rex. The value of this variable is the name of a file where to output additional information (as well as showing it on the standard error). Overall effect is equivalent to setting exp_internal in expect. When rex encounters an error in one of its rc files, it prints the location where the error occurred and a short diagnostic message describing the nature of the error. If you wish to obtain full trace of TCL calls for such errors, use the --tcl-trace option. Normally, this option is only useful for rex developers. OPTIONS
REX RUNrex run PROGRAM ARG [ARG...] The rex run command runs the specified program with the arguments on the servers. Sequential and parallel executionBy default, rex run executes the command on each host in succession. The output from the command is printed on the standard output, preceded by a header line containing the name of the remote host that produced the output, and a colon.The --no-host-header option disables printing of that header line. The -P (--prefix) option further modifies the output by prefixing each line with the hostname, a greater than sign, and a space character. When operating on a big number of hosts, the operation can be sped up by running command on several hosts at once. This parallel operation is enabled by the -jN (--jobs=N) option. The N parameter is the number of hosts to be processed in a batch. The -j option implies --prefix --no-host-header to make it clear which host produced which line. However, if the command produces multiple lines of output, they become intermixed when running in parallel. To further improve readability of the output, the -b (--buffer-output) option is provided. This option instructs rex to buffer output from each host in the batch and to output it only after the host closes connection. This way, output from each host appears on screen as a contiguous text. The -b option cancels the implied --prefix --no-host-header settings. Transferring data prior to executionIf the program relies on some additional files to be present, these can be transferred to each host prior to running the program. The transfer is requested by the -D (--data-file) option. The argument to this option names the file to be copied to the target host.To transfer multiple files, repeat the -D option for each of them. The files supplied with the -D options will be copied to a temporary directory created in /tmp on each target host. That directory will become current working directory when invoking the PROGRAM. The arguments to PROGRAM can refer to the files transferred via the -D option using the syntax {N}, where N is the number of the -D option occurrence (1-based). The {N} is expanded to the file name part of the argument to the corresponding -D option. For example, rex run -D prefix.txt -D repl sed -f {2} {1} /etc/passwd will result in running the following command in a temporary directory on each target host: sed -f repl prefix.txt /etc/passwd Note, that the {N} notation expands to a file name relative to the current working directory on the remote host. This means that rex run -D /tmp/prefix.txt -D repl sed -f {2} {1} /etc/passwd will result in exactly the same command as the example above: the expansion of {1} will throw away the directory part from the file name. Similarly, the token {} expands to a space-separated list of file names passed with all -D options, in the same order they appear on the command line. The PROGRAM itself can be an interpreted script that needs to be copied on each host for execution. The --copy option instructs rex to create a temporary directory on the remote host, copy PROGRAM there, and then select the interpreter to run by analyzing the first line from it. If this line starts with characters #!, the rest of line is taken as the interpreter pathname and initial arguments. For example, assuming that file /libexec/foo is a shell script beginning with #!/bin/ksh, the following command: rex run --copy /libexec/foo /home will first copy the file /libexec/foo to a temporary directory on the remote host, and then invoke it as /bin/ksh foo /home If the file does not begin with #!, the default interpreter, /bin/sh, will be used. The default interpreter can be changed using the --interpreter option, as in the following example: rex run --copy --interpreter="/bin/env perl" test.pl ScriptingWhen given the -s NAME option, rex run looks for the file named NAME.tcl in the library search path. The search path is scanned from left to right. The first encountered NAME.tcl file is sourced in the namespace hostproc.The default library search path is: ~/.rex/script
/usr/local/etc If the -g GROUP (--group=GROUP) option is given, the path is modified as follows: ~/.rex/GROUP/script
/usr/local/etc/GROUP/script ~/.rex/script /usr/local/etc The following functions from the hostproc namespace are used by rex:
proc prologue {} { global config set config(argv) {ls -ld /home} }
proc transition {host state} { if {$state == "INIT"} { puts -nonewline stderr "$host... " } elseif {$state == "LOGOUT"} { puts "done" } }
REX RUN OPTIONS
REX COPYrex copy [OPTIONS] FILE [FILE...] DEST The rex copy command copies one or more FILEs to the given destination (DEST) on each remote host. The command verb can be abbreviated to cp. REX COPY OPTIONS
REX COPY-FROMrex copy-from [OPTIONS] HOST FILE [FILE...] DEST The copy-from command copies the requested files from HOST to the given destination DEST on the local machine. Basically, it is equivalent to running scp with proper arguments, except that it is more convenient to use if HOST doesn't allow for passwordless authentication. In that case rex copy-from will use the authentication credentials from the rex database, as described in the subsection Login credentials, above. The command verb rcp (an abbreviation for reverse copy), can be used instead of copy-from. REX COPY-FROM OPTIONS
REX GROUPThe rex group command manages host group stack.
All options are mutually exclusive. All the commands above accept also the -h (--help) option, which displays a short help summary. REX LOGINrex login [OPTIONS] HOST Logs in to the remote host HOST. It is basically equivalent to using ssh, except that it is convenient to use if the HOST doesn't support passwordless authentication (see the section Login credentials, above). REX LOGIN OPTIONS
REX LISTrex list [groups] rex list OPTIONS hosts List hostgroups or hosts in a hostgroup (second form). The output of rex list groups consists of two columns: the group name and its description. The second form lists hosts in a hostgroup selected by OPTIONS. At least one option must be present. REX LIST OPTIONS
Additionally, the -h, or --help option displays a short help summary. REX EDITrex edit [FILE] Starts the editor defined by the VISUAL (or, if it is unset, EDITOR) environment variable for editing the rex database file FILE (or ~/.rex/db, if used without argument). Upon exit from the editor, the command compares the new content with the existing one. If changed, the user is prompted whether they wish to save the changes. CONFIG ARRAYThe config variable is an associative array keeping the configuration of rex. The rc scripts are free to modify it as they see fit. The valid keys in config are:
REX DATABASERex searches for its database file in the following locations:
Existing files are loaded in that order, so that per-user settings override the system-wide ones. Each db file is a textual file with key-value pairs separated by any amount of whitespace. Each pair occupies a separate line. Empty lines and comments (starting with #) are ignored. The format of the key value is: [GROUP:][HOST:]KEYWORD (square brackets denoting optional parts). The reserved keywords are:
Users can define their own keywords. NOTESDisabling command historyYou might wish to disable the command history on remote hosts for commands executed by rex. Use the shrc setting for that. This setting supplies a list of commands rex will send after it has received the initial command line prompt from the remote machine.The exact list of commands depends on the shell that the remote uses. The simplest way, common for bash, ksh, and zsh is: shrc HISTFILE=/dev/null Similar setting for csh(1) or tcsh(1) is: shrc {set histfile=/dev/null} The drawback of this approach is that the assignment itself and all the built-in commands sent by rex are still retained in history. A more sophisticated way, suitable for both bash and zsh shells is: shrc { if test -n "$HISTFILE" && test -f "$HISTFILE"; \ then \ mv $HISTFILE ${HISTFILE}~; \ head -n -1 ${HISTFILE}~ > $HISTFILE; \ fi} \ {unset HISTFILE} It removes the most recent line from the history file and unsets the HISTFILE variable, thereby preventing the shell from further updating it. (The most recent line contains the command stty -echo, sent by rex at the beginning of each session.) To set history cleaning on a per-host (or per-hostgroup) basis, use the shrc key in the rex database file. ZSHInteraction with zsh(1) poses considerable problems because this shell spits out enormous amounts of escape sequences for colorization and other purposes, which makes it next to impossible to write an efficient regular expression for recognizing command line prompts. To make matters even worse, it ignores any changes made by stty.An ad-hoc way of coping with hosts running zsh is provided by the -Z (--zsh-quirk) command line option. However, it is convenient only for casual usage, because of the slowdown it imposes. The proper way of handling zsh hosts is by configuring the earlycmd setting. This configuration setting declares a list of commands that rex is to send as early as it sees the very first line of output from the host. If all your hosts run zsh, add the following to your rc file: ifmode { run { environ TERM=dumb } } earlycmd {unsetopt ZLE} {PS1='$ '} This will disable the zle module, which is the cause of all evil (TM), reset terminal defaults to decent values and set a proper command line prompt. If only some of your hosts run zsh, you can set the earlycmd option in the rex database file, using the earlycmd key, either for hostgroup or for individual hosts. The value of that option is a whitespace-separated list of TCL strings. For example, if the account on host baboo runs zsh, run rex edit and add the following line: baboo:earlycmd {unsetopt ZLE} {PS1='$ '} Notice that you will still need to have environ TERM=dumb in your rc file. FILES
ENVIRONMENT
RETURN VALUE
EXAMPLE
rex run -g web uptime
rex run -g web --prefix --no-host-header uptime
rex run -g web --sudo /etc/init.d/httpd restart AUTHORSSergey PoznyakoffBUG REPORTSReport bugs to <gray+rex@gnu.org.ua>.COPYRIGHTCopyright © 2012-2022 Sergey PoznyakoffLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
Visit the GSP FreeBSD Man Page Interface. |