2022-03-28 10:05:26 -07:00
|
|
|
#!/bin/bash
|
|
|
|
module_load ssh
|
|
|
|
remote_script () {
|
|
|
|
# usage: remote_script script -- <ssh setup options> host <ssh options>
|
|
|
|
# see ssh function
|
|
|
|
#
|
2023-02-09 09:26:51 -08:00
|
|
|
local sshargs;local user;local supass;local fn;local args;local file
|
|
|
|
|
|
|
|
[[ "$*" =~ "--" ]] && sshargs=$(sed 's/.*--\(.*\)/\1/' <<< "$@")
|
|
|
|
[[ ! $sshargs ]] && { echo missing remote machine, must provide at least a hostname, -- hostname; return 3; }
|
|
|
|
# reset arguments to just those before --
|
|
|
|
set -- $(sed 's/\(.*\)--.*/\1/' <<< "$@")
|
2022-03-28 10:05:26 -07:00
|
|
|
|
|
|
|
local OPTION
|
|
|
|
local OPTARG
|
|
|
|
local OPTIND
|
2023-02-09 09:26:51 -08:00
|
|
|
while getopts 'u:s:f:p:' OPTION; do
|
|
|
|
# echo OPTION $OPTION ARG $OPTARG INDX $OPTIND
|
2022-03-28 10:05:26 -07:00
|
|
|
case "$OPTION" in
|
2023-02-09 09:26:51 -08:00
|
|
|
s)
|
|
|
|
# script filename to run
|
|
|
|
file=$OPTARG
|
|
|
|
;;
|
2022-03-28 10:05:26 -07:00
|
|
|
f)
|
|
|
|
# run a function within the script
|
|
|
|
fn=$OPTARG
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
# run command as another user
|
|
|
|
user=$OPTARG
|
|
|
|
;;
|
2023-02-09 09:26:51 -08:00
|
|
|
p)
|
2022-03-28 10:05:26 -07:00
|
|
|
# password of sudo account for running as root or other user
|
|
|
|
supass=$OPTARG
|
2023-02-09 09:26:51 -08:00
|
|
|
;;
|
2022-03-28 10:05:26 -07:00
|
|
|
*)
|
|
|
|
echo unknown remote script option -$OPTARG
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2023-02-09 09:26:51 -08:00
|
|
|
shift $((OPTIND - 1))
|
2022-03-28 10:05:26 -07:00
|
|
|
|
2023-02-09 09:26:51 -08:00
|
|
|
[[ ! $file ]] && { file=$1;shift 1; }
|
|
|
|
if [[ ! -f $file ]]; then
|
|
|
|
file=$(module_find $file)
|
|
|
|
[[ ! $file ]] && { echo no module or script at $file to run; return 1; }
|
|
|
|
fi
|
2022-03-28 10:05:26 -07:00
|
|
|
|
2023-02-09 09:26:51 -08:00
|
|
|
# remaining script arguments
|
|
|
|
args=$*
|
|
|
|
# echo arguments for script: $args
|
|
|
|
|
|
|
|
script=$file
|
2022-03-28 10:05:26 -07:00
|
|
|
# if script loads a module then use bundler
|
|
|
|
if [[ $(cat $script | grep module_load) ]]; then
|
2023-02-09 09:26:51 -08:00
|
|
|
# echo bundling modules with script
|
2022-03-28 10:05:26 -07:00
|
|
|
local temp
|
|
|
|
module_load bundle
|
|
|
|
script=$( mktemp -t TEMP_FILE_script.XXXXXXXX )
|
2023-02-09 09:26:51 -08:00
|
|
|
bundle $file $script
|
2022-03-28 10:05:26 -07:00
|
|
|
temp=true
|
|
|
|
fi
|
|
|
|
|
|
|
|
local _sudo
|
|
|
|
if [[ $user ]]; then
|
|
|
|
[[ ! $supass ]] && { echo password must be supplied for connecting sudo account. use -s;return 7; }
|
|
|
|
# echo running script remotely as user $user
|
|
|
|
_sudo="echo '${supass}' | sudo --stdin -u ${user} 2>/dev/null"
|
|
|
|
else
|
|
|
|
if [[ $supass ]]; then
|
|
|
|
_sudo="echo '${supass}' | sudo --stdin 2>/dev/null"
|
|
|
|
# echo running script remotely as root
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-02-09 09:26:51 -08:00
|
|
|
src="$(cat $script);"
|
2022-03-28 10:05:26 -07:00
|
|
|
if [[ $fn ]]; then
|
|
|
|
# echo running function
|
2023-02-09 09:26:51 -08:00
|
|
|
cmd="bash -c '$src $fn ${args[*]}'"
|
2022-03-28 10:05:26 -07:00
|
|
|
else
|
|
|
|
# echo running as script
|
|
|
|
local sargs
|
2023-02-09 09:26:51 -08:00
|
|
|
[[ ${args} ]] && sargs="$(printf '%s\n' "set -- ${args}"); "
|
|
|
|
cmd="bash -c '$sargs $src'"
|
2022-03-28 10:05:26 -07:00
|
|
|
fi
|
|
|
|
|
2023-02-09 09:26:51 -08:00
|
|
|
# echo ssh $sshargs "$_sudo" "$cmd"
|
2022-03-28 10:05:26 -07:00
|
|
|
ssh $sshargs "$_sudo" "$cmd"
|
|
|
|
|
|
|
|
[[ $temp ]]&& rm -f $script
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-09 09:26:51 -08:00
|
|
|
|
|
|
|
# can be used with a file or HEREDOC
|
|
|
|
# todo test with < $file or with a pipe
|
|
|
|
remote_stdin () {
|
|
|
|
local file
|
|
|
|
# echo args $@
|
|
|
|
file=$( mktemp -t TEMP_FILE_script.XXXXXXXX )
|
|
|
|
cat > $file
|
|
|
|
echo remote_script -s "$file" "$@"
|
|
|
|
#remote_script -s "$file" "$@"
|
|
|
|
rm $file
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-28 10:05:26 -07:00
|
|
|
# # if script was executed then call the function
|
|
|
|
(return 0 2>/dev/null) ||remote_script $@
|
|
|
|
|
2023-12-14 21:31:40 -08:00
|
|
|
|
|
|
|
|
|
|
|
# sudo rsync --exclude *[C]ache* -e '/usr/bin/ssh -F /home/david/.ssh/_config' --progress -aAru /opt/chromium seldon:/opt
|
|
|
|
|
|
|
|
remote_copy () {
|
|
|
|
|
|
|
|
sudo rsync --exclude *[C]ache* -e '/usr/bin/ssh -F /home/david/.ssh/_config' --progress -aAru /opt/chromium seldon:/opt
|
|
|
|
|
|
|
|
}
|