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() {
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
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
[[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG"
cfg="-F $SSH_CONFIG"
@ -34,17 +56,21 @@ ssh() {
# echo passed: $*
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}
case "$OPTION" in
h)
help
return 3
;;
H)
host=$OPTARG
;;
t)
term=" -t "
;;
d)
dr=true
l)
list=true
;;
r)
ret=true
@ -83,13 +109,12 @@ ssh() {
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
;;
# 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"
help
return 3
# opts="$opts ${@:$OPTIND:1}"
# ((OPTIND+=1))
@ -104,37 +129,24 @@ ssh() {
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)
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"
[[ $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
# run remote commands right here
sshcmd="$sshpass $(which ssh) $term $opts $host"
# echo extra args: "$@"
# echo running command: "$sshcmd"
if [[ $dr ]]; then
if [[ $list ]]; then
echo $sshcmd
else
$sshcmd "$@" || echo SSH Command Failed: $sshcmd "$@"
@ -143,3 +155,13 @@ 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"

View File

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