shell-network/modules/ssh.func

168 lines
4.1 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 list; local sshcmd; local term
local host; local user; local script; local ret ; local key; local efile; local tfile
help() {
cat <<EOF
usage:
ssh <script options> host <SSH command options> <commands to run on remote>
put any additional SSH (man ssh) options after the host, aborting
hH:tu:dF:p:ro:k:
-h
-H
-t
-u
-l
-F
-p
-r
-o
-k
EOF
}
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 'hH:th:u:lF:p:ro:k:' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in
h)
help
return 3
;;
H)
host=$OPTARG
;;
t)
term=" -t "
;;
l)
list=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 script option $OPTARG
help
return 3
# 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 && ! $list ]] && echo host/ip required, aborting && return 2
if [[ $host ]]; then
[[ ! $user ]] && user=$(get_user_host $host)
[[ $user && (! $host =~ "@") ]] && host=$user@$host
if [[ $user && $host =~ "@" ]]; then
host=$(sed 's/.*@\(.*\)/\1/' <<<"$host")
# option takes precedence
host=$user@$host
fi
fi
opts+=" $cfg"
if [[ $ret ]]; then
# return arguments so a command can be composed elsewhere
{ echo "$host,$opts $* ,$sshpass"; return 0; }
else
sshcmd="$sshpass $(which ssh) $term $opts $host"
if [[ $list ]]; then
echo $sshcmd
else
$sshcmd "$@" || echo SSH Command Failed: $sshcmd "$@"
fi
fi
} # end ssh
#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"