diff options
-rw-r--r-- | pars.sh | 70 |
1 files changed, 60 insertions, 10 deletions
@@ -5,9 +5,14 @@ parssh () { _parssh_usage return $? } - local origopts=$- + + local _parssh_origopts=$- set -m + + local _parssh_outfun=_parssh_out local _parssh_prepend_host=true + local _parssh_ssh=_parssh_ssh + while [[ "$1" == -* ]]; do case ${1#-} in [0-9]*) @@ -29,11 +34,30 @@ parssh () { return 98 } local _parssh_rinput="$2" + local _parssh_ssh=_parssh_ssh_rinput shift ;; b|-bare) unset _parssh_prepend_host ;; + o|-out) + touch "${2}.{out,err}" || { + echo "'${2}.{out,err}': unable to write / modify file" + return 97 + } + local _parssh_outprefix="$2" + local _parssh_outfun=_parssh_out_redirect + shift + ;; + t|-tee) + touch "${2}.{out,err}" || { + echo "'${2}.{out,err}': unable to write / modify file" + return 96 + } + local _parssh_outprefix="$2" + local _parssh_outfun=_parssh_out_copy + shift + ;; h|-help) _parssh_usage return $? @@ -52,17 +76,23 @@ parssh () { while (( $(jobs -pr | wc -l) >= ${_parssh_concurrency:-4} )); do sleep 1 done - if [[ -z "$_parssh_rinput" ]]; then - ssh -no StrictHostKeyChecking=no $host "$@" |\ - _parssh_host_prepend & - else - ssh -To StrictHostKeyChecking=no $host "$@" < "$_parssh_rinput" |\ - _parssh_host_prepend & - fi - done <&9 + $_parssh_ssh "$@" 2> >(_parssh_host_prepend >&2) |\ + _parssh_host_prepend & + done <&9 2> >($_parssh_outfun err >&2) |\ + $_parssh_outfun out + wait + exec 9>&- - [[ "${origopts//[^m]/}" == "m" ]] || set +m + [[ "${_parssh_origopts//[^m]/}" == "m" ]] || set +m +} + +_parssh_ssh () { + ssh -no StrictHostKeyChecking=no $host "$@" +} + +_parssh_ssh_rinput () { + ssh -To StrictHostKeyChecking=no $host "$@" < "$_parssh_rinput" } _parssh_host_prepend () @@ -70,8 +100,24 @@ _parssh_host_prepend () printf "${_parssh_prepend_host+$host: }%s\n" "$REPLY" done +_parssh_out () { + cat - +} + +_parssh_out_redirect () { + local _parssh_out_stream="$1" + cat - > "${_parssh_outprefix}.${_parssh_out_stream}" +} + +_parssh_out_copy () { + local _parssh_out_stream="$1" + tee "${_parssh_outprefix}.${_parssh_out_stream}" +} + _parssh_usage () { printf ' %s\n'\ + "parssh: Parallel SSH orchestration in a Bash session."\ + ""\ "SYNOPSIS"\ "parssh [-NUM] [-r|--rinput FILE] [-s|--servers FILE] [-b|--bare] [COMMANDS] [< SERVERS]"\ ""\ @@ -86,6 +132,10 @@ _parssh_usage () { " (Cannot be used in conjunction with a server list on STDIN)."\ " -b, --bare (default: unset/inactive)"\ " Disable prepending of hostname to each output line returned by COMMANDS on SERVERS."\ + " -o PREFIX, --out PREFIX (default: unset/inactive)"\ + " Redirect STDOUT / STDERR to PREFIX.out / PREFIX.err, respectively."\ + " -t PREFIX, --tee PREFIX (default: unset/inactive)"\ + " Copy STDOUT / STDERR to PREFIX.out / PREFIX.err, respectively."\ " COMMANDS"\ " The list of commands to be executed remotely by SSH on each SERVER."\ " < SERVERS"\ |