2022-03-28 10:05:26 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# this will superceed the ssh binary in order to source all the config files
|
|
|
|
# "USAGE: ssh <run/setup options> host <SSH options> <commands to run on remote>"
|
|
|
|
# put any additional SSH (man ssh) options after the host, aborting"#
|
|
|
|
|
|
|
|
# if using ssh in another command use -r flag
|
|
|
|
# this returns a string that can be parsed into an array
|
|
|
|
# the array has three elements
|
|
|
|
# 0 - host (mayb include user)
|
|
|
|
# 1 - all ssh options
|
|
|
|
# 2 - an sshpass command if -p flag was used
|
|
|
|
# to user the arary do this.
|
|
|
|
#
|
|
|
|
# > module_load array
|
|
|
|
# > declare -a ret
|
|
|
|
# > String::split ret "$(ssh -p f filename -m -r test -p 32)"
|
|
|
|
# > host=${ret[0]}; opts=${ret[1]};sshpass=${ret[2]}
|
|
|
|
|
2022-12-26 10:32:29 -08:00
|
|
|
# module_load file
|
2022-03-28 10:05:26 -07:00
|
|
|
module_load net-utils
|
2022-05-02 10:40:07 -07:00
|
|
|
module_load ssh-config
|
2022-03-28 10:05:26 -07:00
|
|
|
|
|
|
|
ssh() {
|
|
|
|
|
2022-12-26 10:32:29 -08:00
|
|
|
local pw;local cfg;local opts;local mp; local sshpass; local dr; local sshcmd
|
|
|
|
local host; local user; local script; local ret ; local key; local efile; local tfile
|
2022-03-28 10:05:26 -07:00
|
|
|
|
|
|
|
if [[ $SSH_CONFIG ]]; then
|
2022-12-26 10:32:29 -08:00
|
|
|
[[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG"
|
|
|
|
cfg="-F $SSH_CONFIG"
|
|
|
|
fi
|
2022-03-28 10:05:26 -07:00
|
|
|
|
|
|
|
# echo passed: $*
|
|
|
|
|
|
|
|
local OPTION; local OPTARG; local OPTIND
|
|
|
|
while getopts ':h:u:mdF:p:ro:k:' OPTION; do
|
|
|
|
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
|
|
|
|
case "$OPTION" in
|
|
|
|
h)
|
|
|
|
host=$OPTARG
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
dr=true
|
|
|
|
;;
|
|
|
|
r)
|
|
|
|
ret=true
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
user=$OPTARG
|
|
|
|
;;
|
|
|
|
k)
|
2022-05-02 10:40:07 -07:00
|
|
|
[[ $(isAbsPath $OPTARG) ]] && key=$OPTARG || key=${SSH_PUB_KEYS:-$HOME/.ssh}/$OPTARG
|
2022-03-28 10:05:26 -07:00
|
|
|
opts+=" -o IdentitiesOnly=yes -o IdentityFile=$key"
|
|
|
|
;;
|
|
|
|
o)
|
|
|
|
opts+=" -o $OPTARG"
|
|
|
|
;;
|
|
|
|
p)
|
|
|
|
# pw=$OPTARG
|
|
|
|
#e, f, d
|
|
|
|
case "$OPTARG" in
|
|
|
|
e)
|
|
|
|
sshpass="sshpass -e "
|
|
|
|
;;
|
|
|
|
f) ;&
|
|
|
|
d)
|
|
|
|
# echo ${@[$OPTIND]}
|
|
|
|
sshpass="sshpass -$OPTARG ${@:$OPTIND:1}"
|
|
|
|
((OPTIND+=1))
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
[[ ! ( $OPTARG = "yes" || $OPTARG = "y" ) ]] && sshpass="sshpass -p $OPTARG"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
pw=true
|
|
|
|
opts+=" -o PreferredAuthentications=password -o PubkeyAuthentication=no -o StrictHostKeyChecking=no"
|
|
|
|
;;
|
|
|
|
F)
|
|
|
|
echo "using SSH Config file at: $OPTARG"
|
|
|
|
[[ -f $OPTARG ]] && cfg="-F $OPTARG" || echo no config file at $OPTARG, ignored
|
|
|
|
;;
|
|
|
|
m)
|
|
|
|
# echo setting multipass
|
|
|
|
mp=true
|
|
|
|
;;
|
|
|
|
*) echo unknown run option -$OPTARG
|
|
|
|
echo "USAGE: ssh <setup options> host <SSH options> <commands to run on remote>"
|
|
|
|
echo "put any additional SSH (man ssh) options after the host, aborting"
|
|
|
|
return 3
|
|
|
|
# opts="$opts ${@:$OPTIND:1}"
|
|
|
|
# ((OPTIND+=1))
|
|
|
|
#echo remaining ${@:$OPTIND}
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
|
|
|
|
[[ (! $host) && $1 ]] && { host=$1;shift; }
|
|
|
|
|
|
|
|
# echo extra ssh options and the command: $@
|
|
|
|
# echo host $host
|
|
|
|
# return
|
|
|
|
|
|
|
|
[[ ! $host ]] && echo host/ip required, aborting && return 2
|
|
|
|
[[ ! $user ]] && user=$(parse_user $host)
|
|
|
|
|
|
|
|
if [[ $mp ]]; then
|
|
|
|
user=${user:-ubuntu}
|
|
|
|
[[ ! $host ]] && echo multipass host/ip required, aborting && return 2
|
|
|
|
# echo multipass host:$host user:$user
|
|
|
|
module_load multipass
|
|
|
|
ip="$(multipass_get_ip $(parse_host $host))"
|
|
|
|
[[ ! $ip ]] && echo could not resolve ip for multipass instance $1 && return 5
|
|
|
|
[[ ! $pw && ! $key ]] && opts+=" $(multipass_ssh_options)"
|
|
|
|
host="$user@$ip"
|
|
|
|
else # not an mp vm
|
|
|
|
[[ $user && (! $host =~ "@") ]] && host=$user@$host
|
|
|
|
if [[ $user && $host =~ "@" ]]; then
|
|
|
|
host=$(sed 's/.*@\(.*\)/\1/' <<<"$host")
|
|
|
|
# option takes precedence
|
|
|
|
host=$user@$host
|
2022-12-26 10:32:29 -08:00
|
|
|
fi
|
2022-03-28 10:05:26 -07:00
|
|
|
opts+=" $cfg"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $ret ]]; then
|
|
|
|
# return arguments so a command can be composed elsewhere
|
|
|
|
{ echo "$host,$opts $* ,$sshpass"; return 0; }
|
|
|
|
else
|
|
|
|
# run remote commands right here
|
|
|
|
sshcmd="$sshpass $(which ssh) $opts $host"
|
|
|
|
# echo extra args: "$@"
|
2022-12-26 10:32:29 -08:00
|
|
|
# echo running command: "$sshcmd"
|
|
|
|
[[ ! $dr ]] && $sshcmd "$@" || echo SSH Command Failed: $sshcmd "$@"
|
2022-03-28 10:05:26 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
} # end ssh
|
|
|
|
|