cnssh: Copy config array to runtime array and work on that instead

This commit is contained in:
David Thurstenson 2021-05-06 17:16:45 -05:00
parent 7e9aa6fdb5
commit e8da138c36
1 changed files with 31 additions and 21 deletions

View File

@ -24,9 +24,16 @@ cnssh() { # Helper for sshing into Crestron devices
#### ####
# Config init # Config init
# Initialize runtime config array
declare -A conf
# Initialize config array if it doesn't already exist # Initialize config array if it doesn't already exist
if [[ ! -v cnssh_conf[@] ]]; then if [[ ! -v cnssh_conf[@] ]]; then
declare -A cnssh_conf declare -A cnssh_conf
else
for opt in "${!cnssh_conf[@]}"; do
conf[$opt]=${cnssh_conf[$opt]}
done
fi fi
# Initialize and populate default config array # Initialize and populate default config array
@ -37,6 +44,14 @@ cnssh() { # Helper for sshing into Crestron devices
[pass]="" [pass]=""
) )
# Apply defaults to running config
for opt in "${!dconf[@]}"; do
# Don't apply the default if it's already set
if [[ ! -v conf[$opt] ]]; then
conf[$opt]=${dconf[$opt]}
fi
done
declare -A sshopts declare -A sshopts
sshopts=( sshopts=(
[StrictHostKeyChecking]="no" [StrictHostKeyChecking]="no"
@ -55,7 +70,7 @@ cnssh() { # Helper for sshing into Crestron devices
while getopts ":u:" opt; do while getopts ":u:" opt; do
case $opt in case $opt in
u) u)
cnssh_conf[uname]="$OPTARG" conf[uname]="$OPTARG"
;; ;;
\?) \?)
echo "Unknown option -$OPTARG" echo "Unknown option -$OPTARG"
@ -73,38 +88,30 @@ cnssh() { # Helper for sshing into Crestron devices
# Set the address based on the first # Set the address based on the first
# arg if not already set in config array # arg if not already set in config array
if [[ ! -v cnssh_conf[addr] ]]; then if [[ ! -v conf[addr] ]]; then
cnssh_conf[addr]="$1" conf[addr]="$1"
shift 1 shift 1
fi fi
# Set the command as the rest of argv # Set the command as the rest of argv
# if not already set in config array # if not already set in config array
if [[ ! -v cnssh_conf[cmd] ]]; then if [[ ! -v conf[cmd] ]]; then
cnssh_conf[cmd]="$*" conf[cmd]="$*"
fi fi
# Apply defaults to running config
for opt in "${!dconf[@]}"; do
# Don't apply the default if it's already set
if [[ ! -v cnssh_conf[$opt] ]]; then
cnssh_conf[$opt]=${dconf[$opt]}
fi
done
# Set ssh user name # Set ssh user name
sshopts[User]="${cnssh_conf[uname]}" sshopts[User]="${conf[uname]}"
# Set ssh host if not using sftp mode # Set ssh host if not using sftp mode
case ${cnssh_conf[method]} in case ${conf[method]} in
ssh) ssh)
: :
;; ;;
sftp) sftp)
unset "cnssh_conf[cmd]" unset "conf[cmd]"
;; ;;
*) *)
echo "Unknown method: ${cnssh_conf[method]}" echo "Unknown method: ${conf[method]}"
;; ;;
esac esac
@ -115,15 +122,15 @@ cnssh() { # Helper for sshing into Crestron devices
#### ####
# Main # Main
sshpass -p "${cnssh_conf[pass]}" \ sshpass -p "${conf[pass]}" \
"${cnssh_conf[method]}" \ "${conf[method]}" \
$( # Format sshopts as a string that an ssh program understands $( # Format sshopts as a string that an ssh program understands
for opt in "${!sshopts[@]}"; do for opt in "${!sshopts[@]}"; do
printf '%s %s=%s ' "-o" "$opt" "${sshopts[$opt]}" printf '%s %s=%s ' "-o" "$opt" "${sshopts[$opt]}"
done done
) \ ) \
"${cnssh_conf[addr]}" \ "${conf[addr]}" \
"${cnssh_conf[cmd]}" "${conf[cmd]}"
# Main # Main
#### ####
@ -141,4 +148,7 @@ cnsftp() {
cnssh_conf[addr]="$*" cnssh_conf[addr]="$*"
cnssh cnssh
unset "cnssh_conf[method]"
unset "cnssh_conf[addr]"
} }