shell-network/modules/ssh.func

146 lines
4.0 KiB
Plaintext
Raw Normal View History

#!/bin/bash
# this will superceed the ssh binary in order to source all the config files
# "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
# 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]}
# module_load file
module_load net-utils
module_load ssh-config
ssh() {
local pw;local cfg;local opts;local mp; local sshpass; local dr; local sshcmd; local term
local host; local user; local script; local ret ; local key; local efile; local tfile
if [[ $SSH_CONFIG ]]; then
[[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG"
cfg="-F $SSH_CONFIG"
fi
# echo passed: $*
local OPTION; local OPTARG; local OPTIND
while getopts 'th:u:mdF:p:ro:k:' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in
h)
host=$OPTARG
;;
t)
term=" -t "
;;
d)
dr=true
;;
r)
ret=true
;;
u)
user=$OPTARG
;;
k)
[[ $(isAbsPath $OPTARG) ]] && key=$OPTARG || key=${SSH_PUB_KEYS:-$HOME/.ssh}/$OPTARG
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; }
debug extra ssh options and the remote commands: $@
[[ ! $host ]] && echo host/ip required, aborting && return 2
[[ ! $user ]] && user=$(get_user_host $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 $(get_hostname_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
fi
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) $term $opts $host"
# echo extra args: "$@"
# echo running command: "$sshcmd"
if [[ $dr ]]; then
echo $sshcmd
else
$sshcmd "$@" || echo SSH Command Failed: $sshcmd "$@"
fi
fi
} # end ssh