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
declare -A dconf
dconf=(
[method]="ssh"
[uname]="Crestron"
[pass]=""
)
declare -A sshopts
sshopts=(
[StrictHostKeyChecking]="no"
[GlobalKnownHostsFile]="/dev/null"
[UserKnownHostsFile]="/dev/null"
[LogLevel]="ERROR"
)
# Config init
####
@ -83,6 +92,26 @@ cnssh() { # Helper for sshing into Crestron devices
fi
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
####
@ -90,13 +119,10 @@ cnssh() { # Helper for sshing into Crestron devices
####
# Main
sshpass -p "${cnssh_conf[pass]}" ssh \
-o StrictHostKeyChecking=no\
-o GlobalKnownHostsFile=/dev/null\
-o UserKnownHostsFile=/dev/null\
-o LogLevel=ERROR\
-o User="${cnssh_conf[uname]}"\
"${cnssh_conf[addr]}" "${cnssh_conf[cmd]}"
sshpass -p "${cnssh_conf[pass]}" \
"${cnssh_conf[method]}" \
$sshoptstring \
"${cnssh_conf[cmd]}"
# Main
####
@ -104,15 +130,14 @@ cnssh() { # Helper for sshing into Crestron devices
cnsftp() {
local uname="Crestron"
local pass=""
local cmd="$@"
# Initialize config array if it doesn't already exist
if [[ ! -v cnssh_conf[@] ]]; then
declare -A cnssh_conf
fi
sshpass -p "$pass" sftp \
-o StrictHostKeyChecking=no\
-o GlobalKnownHostsFile=/dev/null\
-o UserKnownHostsFile=/dev/null\
-o LogLevel=ERROR\
-o User="$uname" \
"$cmd"
# Set sftp mode instead of ssh
cnssh_conf[method]="sftp"
cnssh_conf[cmd]="$*"
cnssh
}