Another option to a script by Arcege would be a Bash function:
sshbatch() {
# Expect at least 2 parameters, if less are provided print help
if [[ ${#@} -lt 2 ]]; then
printf 'Usage: sshbatch [user@]host... input_file\n'
else
while read -r -u "$fd" host; do
# Check if the last parameter is a readable file, else print error and exit
[[ -r ${@:(-1)} ]] || { printf "The file ${@:(-1)} is not readable!\n"; break; }
# Run remote bash from the file given in the last parameter
ssh -o BatchMode=yes "$host" bash -s < "${@:(-1)}"
# Read host list from 1st to next to last parameters
done {fd}< <(printf '%s\n' "${@:1:${#@}-1}")
fi
}
I don't use ssh's -T
option, as it doesn't work with it in all cases.
This script uses parameter expansion on positional parameters array $@
:
"${@:(-1)}"
expands to the last parameter (literally first form the end onwards)
"${#@}"
expands to the number of positional parameters
"${@:1:${#@}-1}"
expands to a list of all parameters from first to next to last (literally number of parameters less one).