#!/bin/bash # this will superceed the ssh binary in order to source all the config files # "USAGE: ssh " # 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 < host put any additional SSH (man ssh) options can be added with -s, be sure to quote :hH:tu:dF:p:ro:k: -h -H -t -u -l -F -p -r -s -o -k-z no EOF } if [[ $SSH_CONFIG ]]; then [[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG" cfg="-F $SSH_CONFIG" fi local OPTION; local OPTARG; local OPTIND while getopts 'hH:th:u:lF:p:ro:k:' OPTION; do # echo ssh passed: "$@" debug processing: option:$OPTION argument:$OPTARG index:$OPTIND # echo 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=$OPTARGF #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 ;; *) echo unknown ssh script option $OPTARG xxx help return 3 # opts+=" ${@:$OPTIND:1}" # ((OPTIND+=1)) #echo remaining ${@:$OPTIND} ;; esac done shift $((OPTIND - 1)) [[ (! $host) && $1 ]] && { host=$1;shift; } debug remote commands/arguments to pass: $@ debug todo parse additional options after -- [[ ! $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"