cnssh: refactor so cnsftp can reuse cnssh for sftp mode

This commit is contained in:
David Thurstenson 2021-05-06 15:40:50 -05:00
parent db7a784d9f
commit 397f7bd166
1 changed files with 42 additions and 17 deletions

View File

@ -32,10 +32,19 @@ cnssh() { # Helper for sshing into Crestron devices
# Initialize and populate default config array # Initialize and populate default config array
declare -A dconf declare -A dconf
dconf=( dconf=(
[method]="ssh"
[uname]="Crestron" [uname]="Crestron"
[pass]="" [pass]=""
) )
declare -A sshopts
sshopts=(
[StrictHostKeyChecking]="no"
[GlobalKnownHostsFile]="/dev/null"
[UserKnownHostsFile]="/dev/null"
[LogLevel]="ERROR"
)
# Config init # Config init
#### ####
@ -83,6 +92,26 @@ cnssh() { # Helper for sshing into Crestron devices
fi fi
done done
# Set ssh user name
sshopts[User]="${cnssh_conf[uname]}"
# Set ssh host if not using sftp mode
case ${cnssh_conf[method]} in
ssh)
sshopts[Host]="${cnssh_conf[addr]}"
;;
sftp)
:
;;
*)
echo "Unknown method: ${cnssh_conf[method]}"
;;
esac
for opt in "${!sshopts[@]}"; do
sshoptstring="$sshoptstring -o $opt=${sshopts[$opt]}"
done
# Option Parsing # Option Parsing
#### ####
@ -90,13 +119,10 @@ cnssh() { # Helper for sshing into Crestron devices
#### ####
# Main # Main
sshpass -p "${cnssh_conf[pass]}" ssh \ sshpass -p "${cnssh_conf[pass]}" \
-o StrictHostKeyChecking=no\ "${cnssh_conf[method]}" \
-o GlobalKnownHostsFile=/dev/null\ $sshoptstring \
-o UserKnownHostsFile=/dev/null\ "${cnssh_conf[cmd]}"
-o LogLevel=ERROR\
-o User="${cnssh_conf[uname]}"\
"${cnssh_conf[addr]}" "${cnssh_conf[cmd]}"
# Main # Main
#### ####
@ -104,15 +130,14 @@ cnssh() { # Helper for sshing into Crestron devices
cnsftp() { cnsftp() {
local uname="Crestron" # Initialize config array if it doesn't already exist
local pass="" if [[ ! -v cnssh_conf[@] ]]; then
local cmd="$@" declare -A cnssh_conf
fi
sshpass -p "$pass" sftp \ # Set sftp mode instead of ssh
-o StrictHostKeyChecking=no\ cnssh_conf[method]="sftp"
-o GlobalKnownHostsFile=/dev/null\ cnssh_conf[cmd]="$*"
-o UserKnownHostsFile=/dev/null\
-o LogLevel=ERROR\ cnssh
-o User="$uname" \
"$cmd"
} }