ssh: add help, add list option for using ssh command in other functions

removed multipass code

ammended
master
David Kebler 2024-03-01 15:06:34 -08:00
parent 960790195c
commit c407e99b25
2 changed files with 59 additions and 35 deletions

View File

@ -23,9 +23,31 @@ module_load ssh-config
ssh() { ssh() {
local pw;local cfg;local opts;local mp; local sshpass; local dr; local sshcmd; local term 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 local host; local user; local script; local ret ; local key; local efile; local tfile
help() {
cat <<EOF
usage:
ssh <setup options> host <SSH 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 if [[ $SSH_CONFIG ]]; then
[[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG" [[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG"
cfg="-F $SSH_CONFIG" cfg="-F $SSH_CONFIG"
@ -34,17 +56,21 @@ ssh() {
# echo passed: $* # echo passed: $*
local OPTION; local OPTARG; local OPTIND local OPTION; local OPTARG; local OPTIND
while getopts 'th:u:mdF:p:ro:k:' OPTION; do while getopts 'hH:th:u:lF:p:ro:k:' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} # echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in case "$OPTION" in
h) h)
help
return 3
;;
H)
host=$OPTARG host=$OPTARG
;; ;;
t) t)
term=" -t " term=" -t "
;; ;;
d) l)
dr=true list=true
;; ;;
r) r)
ret=true ret=true
@ -83,13 +109,12 @@ ssh() {
echo "using SSH Config file at: $OPTARG" echo "using SSH Config file at: $OPTARG"
[[ -f $OPTARG ]] && cfg="-F $OPTARG" || echo no config file at $OPTARG, ignored [[ -f $OPTARG ]] && cfg="-F $OPTARG" || echo no config file at $OPTARG, ignored
;; ;;
m) # m)
# echo setting multipass # # echo setting multipass
mp=true # mp=true
;; # ;;
*) echo unknown run option -$OPTARG *) echo unknown run option -$OPTARG
echo "USAGE: ssh <setup options> host <SSH options> <commands to run on remote>" help
echo "put any additional SSH (man ssh) options after the host, aborting"
return 3 return 3
# opts="$opts ${@:$OPTIND:1}" # opts="$opts ${@:$OPTIND:1}"
# ((OPTIND+=1)) # ((OPTIND+=1))
@ -104,37 +129,24 @@ ssh() {
debug extra ssh options and the remote commands: $@ debug extra ssh options and the remote commands: $@
[[ ! $host ]] && echo host/ip required, aborting && return 2 [[ ! $host && ! $list ]] && echo host/ip required, aborting && return 2
if [[ $host ]]; then
[[ ! $user ]] && user=$(get_user_host $host) [[ ! $user ]] && user=$(get_user_host $host)
[[ $user && (! $host =~ "@") ]] && host=$user@$host
if [[ $mp ]]; then if [[ $user && $host =~ "@" ]]; then
user=${user:-ubuntu} host=$(sed 's/.*@\(.*\)/\1/' <<<"$host")
[[ ! $host ]] && echo multipass host/ip required, aborting && return 2 # option takes precedence
# echo multipass host:$host user:$user host=$user@$host
module_load multipass fi
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 fi
opts+=" $cfg"
if [[ $ret ]]; then if [[ $ret ]]; then
# return arguments so a command can be composed elsewhere # return arguments so a command can be composed elsewhere
{ echo "$host,$opts $* ,$sshpass"; return 0; } { echo "$host,$opts $* ,$sshpass"; return 0; }
else else
# run remote commands right here
sshcmd="$sshpass $(which ssh) $term $opts $host" sshcmd="$sshpass $(which ssh) $term $opts $host"
# echo extra args: "$@" if [[ $list ]]; then
# echo running command: "$sshcmd"
if [[ $dr ]]; then
echo $sshcmd echo $sshcmd
else else
$sshcmd "$@" || echo SSH Command Failed: $sshcmd "$@" $sshcmd "$@" || echo SSH Command Failed: $sshcmd "$@"
@ -143,3 +155,13 @@ fi
} # end ssh } # 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"

View File

@ -69,6 +69,8 @@ function smount() {
# opts+=" $scmd" # opts+=" $scmd"
# remove_end_spaces "$scmd" # remove_end_spaces "$scmd"
# return # return
# must remote spaces or sshfs fails
opts+=" -o ssh_command=$(remove_end_spaces "'$sshpass /usr/bin/ssh $sshopts'")" opts+=" -o ssh_command=$(remove_end_spaces "'$sshpass /usr/bin/ssh $sshopts'")"
# [[ ! $(host_reachable $host $(parse_option $sshopts -p)) ]] \ # [[ ! $(host_reachable $host $(parse_option $sshopts -p)) ]] \