2022-03-28 10:05:26 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# this will superceed the ssh binary in order to source all the config files
|
2024-02-21 11:33:37 -08:00
|
|
|
# "USAGE: ssh <ssh script options> <host,can set via -h> <SSH options> <commands to run on remote>"
|
|
|
|
# put any additional SSH (man ssh) options after the host
|
2022-03-28 10:05:26 -07:00
|
|
|
|
|
|
|
# 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() {
|
|
|
|
|
2024-03-01 15:06:34 -08:00
|
|
|
local pw;local cfg;local opts;local mp; local sshpass; local list; local sshcmd; local term
|
2022-12-26 10:32:29 -08:00
|
|
|
local host; local user; local script; local ret ; local key; local efile; local tfile
|
2022-03-28 10:05:26 -07:00
|
|
|
|
2024-03-01 15:06:34 -08:00
|
|
|
help() {
|
|
|
|
|
|
|
|
cat <<EOF
|
|
|
|
usage:
|
2024-12-08 12:07:57 -08:00
|
|
|
ssh <script options> host <commands to run on remote>
|
|
|
|
put any additional SSH (man ssh) options can be added with -s, be sure to quote
|
|
|
|
:hH:tu:dF:p:ro:k:
|
2024-03-01 15:06:34 -08:00
|
|
|
-h
|
|
|
|
-H
|
|
|
|
-t
|
|
|
|
-u
|
|
|
|
-l
|
|
|
|
-F
|
|
|
|
-p
|
|
|
|
-r
|
2024-12-08 12:07:57 -08:00
|
|
|
-s
|
2024-03-01 15:06:34 -08:00
|
|
|
-o
|
2024-12-08 12:07:57 -08:00
|
|
|
-k-z no
|
2024-03-01 15:06:34 -08:00
|
|
|
EOF
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
local OPTION; local OPTARG; local OPTIND
|
2024-03-01 15:06:34 -08:00
|
|
|
while getopts 'hH:th:u:lF:p:ro:k:' OPTION; do
|
2024-12-08 12:07:57 -08:00
|
|
|
# echo ssh passed: "$@"
|
|
|
|
debug processing: option:$OPTION argument:$OPTARG index:$OPTIND
|
|
|
|
# echo remaining:${@:$OPTIND}
|
2022-03-28 10:05:26 -07:00
|
|
|
case "$OPTION" in
|
2024-03-01 15:06:34 -08:00
|
|
|
h)
|
|
|
|
help
|
|
|
|
return 3
|
|
|
|
;;
|
|
|
|
H)
|
2022-03-28 10:05:26 -07:00
|
|
|
host=$OPTARG
|
|
|
|
;;
|
2024-02-21 11:33:37 -08:00
|
|
|
t)
|
|
|
|
term=" -t "
|
|
|
|
;;
|
2024-03-01 15:06:34 -08:00
|
|
|
l)
|
|
|
|
list=true
|
2022-03-28 10:05:26 -07:00
|
|
|
;;
|
|
|
|
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"
|
2024-12-08 12:07:57 -08:00
|
|
|
;;
|
2022-03-28 10:05:26 -07:00
|
|
|
o)
|
|
|
|
opts+=" -o $OPTARG"
|
|
|
|
;;
|
|
|
|
p)
|
2024-12-08 12:07:57 -08:00
|
|
|
# pw=$OPTARGF
|
2022-03-28 10:05:26 -07:00
|
|
|
#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
|
|
|
|
;;
|
2024-03-01 15:06:34 -08:00
|
|
|
# m)
|
|
|
|
# # echo setting multipass
|
|
|
|
# mp=true
|
|
|
|
# ;;
|
2024-03-13 09:20:42 -07:00
|
|
|
*)
|
2024-12-08 12:07:57 -08:00
|
|
|
echo unknown ssh script option $OPTARG xxx
|
2024-03-01 15:06:34 -08:00
|
|
|
help
|
2022-03-28 10:05:26 -07:00
|
|
|
return 3
|
2024-03-13 09:20:42 -07:00
|
|
|
# opts+=" ${@:$OPTIND:1}"
|
2022-03-28 10:05:26 -07:00
|
|
|
# ((OPTIND+=1))
|
|
|
|
#echo remaining ${@:$OPTIND}
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
|
|
|
|
[[ (! $host) && $1 ]] && { host=$1;shift; }
|
|
|
|
|
2024-12-08 12:07:57 -08:00
|
|
|
debug remote commands/arguments to pass: $@
|
|
|
|
debug todo parse additional options after --
|
2024-02-21 11:33:37 -08:00
|
|
|
|
2024-03-01 15:06:34 -08:00
|
|
|
[[ ! $host && ! $list ]] && echo host/ip required, aborting && return 2
|
|
|
|
if [[ $host ]]; then
|
2024-02-21 11:33:37 -08:00
|
|
|
[[ ! $user ]] && user=$(get_user_host $host)
|
2024-03-01 15:06:34 -08:00
|
|
|
[[ $user && (! $host =~ "@") ]] && host=$user@$host
|
|
|
|
if [[ $user && $host =~ "@" ]]; then
|
|
|
|
host=$(sed 's/.*@\(.*\)/\1/' <<<"$host")
|
|
|
|
# option takes precedence
|
|
|
|
host=$user@$host
|
|
|
|
fi
|
2022-03-28 10:05:26 -07:00
|
|
|
fi
|
2024-03-01 15:06:34 -08:00
|
|
|
opts+=" $cfg"
|
|
|
|
|
2022-03-28 10:05:26 -07:00
|
|
|
if [[ $ret ]]; then
|
|
|
|
# return arguments so a command can be composed elsewhere
|
|
|
|
{ echo "$host,$opts $* ,$sshpass"; return 0; }
|
|
|
|
else
|
2024-12-08 12:07:57 -08:00
|
|
|
sshcmd="$sshpass $(which ssh) $term $opts $host $*"
|
2024-03-01 15:06:34 -08:00
|
|
|
if [[ $list ]]; then
|
2024-12-08 12:07:57 -08:00
|
|
|
echo "$sshcmd"
|
2024-02-21 11:33:37 -08:00
|
|
|
else
|
2024-12-08 12:07:57 -08:00
|
|
|
$sshcmd || echo SSH Command Failed: $sshcmd
|
2024-02-21 11:33:37 -08:00
|
|
|
fi
|
2022-03-28 10:05:26 -07:00
|
|
|
fi
|
|
|
|
|
|
|
|
} # end ssh
|
|
|
|
|
2024-03-01 15:06:34 -08:00
|
|
|
|
|
|
|
#TODO make this a function for multipass module
|
|
|
|
# user=${user:-ubuntu}
|
|
|
|
# [[ ! $host && ! $list ]] && echo multipass host/ip required, aborting && return 2
|
|
|
|
# # echo multipass host:$host user:$user
|
|
|
|
# module_load multipass
|
|
|
|
# ip="$(multipass_get_ip $(get_hostname_host $host))"
|
|
|
|
# [[ ! $ip && ! $list ]] && echo could not resolve ip for multipass instance $1 && return 5
|
|
|
|
# [[ ! $pw && ! $key ]] && opts+=" $(multipass_ssh_options)"
|
|
|
|
# host="$user@$ip"
|