2021-05-06 19:25:10 +00:00
|
|
|
cnssh() { # Helper for sshing into Crestron devices
|
|
|
|
#
|
|
|
|
# USAGE
|
|
|
|
#
|
|
|
|
# cnssh <ip address> [command]
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# OPTIONS
|
|
|
|
#
|
|
|
|
# -u <username> Specify the username to use with ssh
|
|
|
|
# Default: "Crestron"
|
|
|
|
#
|
|
|
|
# CONFIG ARRAY: cnssh_conf
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# | Option | Valid Args | Default | Description |
|
|
|
|
# | ------------- | ------------- | ------------- | --------------------------------------------- |
|
|
|
|
# | uname | <string> | "Crestron" | The username for use with ssh |
|
|
|
|
# | pass | <string> | "" | The password for use with ssh (via sshpass) |
|
|
|
|
# | addr | <string> | <ip address> | The address of the remote device |
|
|
|
|
# | cmd | <string> | [command] | The command to execute on the remote device |
|
|
|
|
|
|
|
|
|
|
|
|
####
|
|
|
|
# Config init
|
|
|
|
|
2021-05-06 22:16:45 +00:00
|
|
|
# Initialize runtime config array
|
|
|
|
declare -A conf
|
|
|
|
|
2021-05-06 19:25:10 +00:00
|
|
|
# Initialize config array if it doesn't already exist
|
|
|
|
if [[ ! -v cnssh_conf[@] ]]; then
|
|
|
|
declare -A cnssh_conf
|
2021-05-06 22:16:45 +00:00
|
|
|
else
|
|
|
|
for opt in "${!cnssh_conf[@]}"; do
|
|
|
|
conf[$opt]=${cnssh_conf[$opt]}
|
|
|
|
done
|
2021-05-06 19:25:10 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Initialize and populate default config array
|
|
|
|
declare -A dconf
|
|
|
|
dconf=(
|
2021-05-06 20:40:50 +00:00
|
|
|
[method]="ssh"
|
2021-05-06 19:25:10 +00:00
|
|
|
[uname]="Crestron"
|
|
|
|
[pass]=""
|
|
|
|
)
|
|
|
|
|
2021-05-06 22:16:45 +00:00
|
|
|
# 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
|
|
|
|
|
2021-05-06 20:40:50 +00:00
|
|
|
declare -A sshopts
|
|
|
|
sshopts=(
|
|
|
|
[StrictHostKeyChecking]="no"
|
|
|
|
[GlobalKnownHostsFile]="/dev/null"
|
|
|
|
[UserKnownHostsFile]="/dev/null"
|
|
|
|
[LogLevel]="ERROR"
|
|
|
|
)
|
|
|
|
|
2021-05-06 19:25:10 +00:00
|
|
|
# Config init
|
|
|
|
####
|
|
|
|
|
|
|
|
|
|
|
|
####
|
|
|
|
# Option Parsing
|
|
|
|
|
|
|
|
while getopts ":u:" opt; do
|
|
|
|
case $opt in
|
|
|
|
u)
|
2021-05-06 22:16:45 +00:00
|
|
|
conf[uname]="$OPTARG"
|
2021-05-06 19:25:10 +00:00
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "Unknown option -$OPTARG"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
echo "Option requires argument: -$OPTARG"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Remove parsed options from $@
|
|
|
|
shift $((OPTIND -1))
|
|
|
|
|
|
|
|
# Set the address based on the first
|
|
|
|
# arg if not already set in config array
|
2021-05-06 22:16:45 +00:00
|
|
|
if [[ ! -v conf[addr] ]]; then
|
|
|
|
conf[addr]="$1"
|
2021-05-06 19:25:10 +00:00
|
|
|
shift 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set the command as the rest of argv
|
|
|
|
# if not already set in config array
|
2021-05-06 22:16:45 +00:00
|
|
|
if [[ ! -v conf[cmd] ]]; then
|
|
|
|
conf[cmd]="$*"
|
2021-05-06 19:25:10 +00:00
|
|
|
fi
|
|
|
|
|
2021-05-06 20:40:50 +00:00
|
|
|
# Set ssh user name
|
2021-05-06 22:16:45 +00:00
|
|
|
sshopts[User]="${conf[uname]}"
|
2021-05-06 20:40:50 +00:00
|
|
|
|
|
|
|
# Set ssh host if not using sftp mode
|
2021-05-06 22:16:45 +00:00
|
|
|
case ${conf[method]} in
|
2021-05-06 20:40:50 +00:00
|
|
|
ssh)
|
2021-05-06 21:25:04 +00:00
|
|
|
:
|
2021-05-06 20:40:50 +00:00
|
|
|
;;
|
|
|
|
sftp)
|
2021-05-06 22:16:45 +00:00
|
|
|
unset "conf[cmd]"
|
2021-05-06 20:40:50 +00:00
|
|
|
;;
|
|
|
|
*)
|
2021-05-06 22:16:45 +00:00
|
|
|
echo "Unknown method: ${conf[method]}"
|
2021-05-06 20:40:50 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2021-05-06 19:25:10 +00:00
|
|
|
# Option Parsing
|
|
|
|
####
|
|
|
|
|
|
|
|
|
|
|
|
####
|
|
|
|
# Main
|
|
|
|
|
2021-05-06 22:16:45 +00:00
|
|
|
sshpass -p "${conf[pass]}" \
|
|
|
|
"${conf[method]}" \
|
2021-05-06 20:53:26 +00:00
|
|
|
$( # Format sshopts as a string that an ssh program understands
|
|
|
|
for opt in "${!sshopts[@]}"; do
|
|
|
|
printf '%s %s=%s ' "-o" "$opt" "${sshopts[$opt]}"
|
|
|
|
done
|
|
|
|
) \
|
2021-05-06 22:16:45 +00:00
|
|
|
"${conf[addr]}" \
|
|
|
|
"${conf[cmd]}"
|
2021-05-06 19:25:10 +00:00
|
|
|
|
|
|
|
# Main
|
|
|
|
####
|
2019-12-11 17:46:30 +00:00
|
|
|
}
|
2021-05-06 19:39:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
cnsftp() {
|
2021-05-06 20:40:50 +00:00
|
|
|
# Initialize config array if it doesn't already exist
|
|
|
|
if [[ ! -v cnssh_conf[@] ]]; then
|
|
|
|
declare -A cnssh_conf
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Set sftp mode instead of ssh
|
|
|
|
cnssh_conf[method]="sftp"
|
2021-05-06 21:25:04 +00:00
|
|
|
cnssh_conf[addr]="$*"
|
2021-05-06 20:40:50 +00:00
|
|
|
|
|
|
|
cnssh
|
2021-05-06 22:16:45 +00:00
|
|
|
|
|
|
|
unset "cnssh_conf[method]"
|
|
|
|
unset "cnssh_conf[addr]"
|
2021-05-06 19:39:22 +00:00
|
|
|
}
|