Skip to content

Instantly share code, notes, and snippets.

@samson4649
Last active September 9, 2020 04:30
Show Gist options
  • Save samson4649/1da46c76a099da6757a50a47a0c4a999 to your computer and use it in GitHub Desktop.
Save samson4649/1da46c76a099da6757a50a47a0c4a999 to your computer and use it in GitHub Desktop.
SSH config management file to enable and disable ssh configs on the fly
#!/bin/bash
##################################
#
# SSH Mod Config Script
# v1.2
#
# @github.com/samson4649
#
#
##################################
#
# Setup
#
# move script to bin location
# $ mv ssh-mod-conf.sh /usr/local/bin/ssh-mod-conf
#
# add multi name link calls
# $ ln -s /usr/local/bin/ssh-mod-conf /usr/local/bin/sshlsconf
# $ ln -s /usr/local/bin/ssh-mod-conf /usr/local/bin/sshenconf
# $ ln -s /usr/local/bin/ssh-mod-conf /usr/local/bin/sshdisconf
#
# building support directories
# $ mkdir ${HOME}/.ssh/config.d/{available,enabled}.d
#
# move current configuiration to new directory
# $ mv ~/.ssh/config ~/.ssh/config.d/available.d/default.conf
#
# enable the default configuration
# $ sshenconf default
#
# set to include the enabled.d directory
# $ echo "Include ~/.ssh/config.d/available.d/*.conf" > ~/.ssh/config
#
#
# #### OPTIONAL #####
#
# (optional) add additional configurations to available.d directory with the ".conf" suffix (wont work without it)
# $ mv site-a.conf ~/.ssh/config.d/available.d/site-a.conf
#
# check that the configuration is enabled
# $ sshlsconf
# [ENABLED] /home/samson4649/.ssh/available.d/default.conf
# [DISABLED] /home/samson4649/.ssh/available.d/site-a.conf
#
###################################
EXEC="${0##*/}"
conf="${1}"
function _exit() {
ECODE=$1
shift
echo "$@"
#echo "exiting with error code ${ECODE}"
exit ${ECODE}
}
function _check_available(){
if [ -e "${HOME}/.ssh/config.d/available.d/${conf}.conf" ]; then
echo "true"
else
echo "false"
fi
}
function _check_enabled(){
if [ -e "${HOME}/.ssh/config.d/enabled.d/${conf}.conf" ]; then
echo "true"
else
echo "false"
fi
}
function _check_empty(){
if [ -z "${conf}" ]; then
_exit 99 "Cant use an empty configuration file..."
fi
}
case "${EXEC}" in
sshlsconf)
find "${HOME}"/.ssh/config.d/available.d -name "*.conf" | grep .conf &>/dev/null \
|| _exit 2 "No available configuration in available.d"
while IFS= read -r c; do
if [ -e "${HOME}/.ssh/config.d/enabled.d/${c##*/}" ]; then
echo -ne "[\e[32mENABLED\e[39m] "
else
echo -n "[DISABLED] "
fi
echo "${c}"
done < <(ls -w 1 "${HOME}"/.ssh/config.d/available.d/*.conf )
;;
sshenconf)
_check_empty
if [[ "$(_check_available)" == "true" ]]; then
if [[ "$(_check_enabled)" == "true" ]] ; then
_exit 1 "Already enabled config '${conf}'"
else
ln -s "${HOME}/.ssh/config.d/available.d/${conf}.conf" \
"${HOME}/.ssh/config.d/enabled.d/${conf}.conf" && \
echo "Config '${conf}' enabled"
fi
else
echo "Not able to find config '${conf}' in ${HOME}/.ssh/config.d/available.d/"
fi
;;
sshdisconf)
_check_empty
if [[ "$(_check_enabled)" == "false" ]] ; then
_exit 1 "Config not enabled - '${conf}'"
else
rm -f "${HOME}/.ssh/config.d/enabled.d/${conf}.conf" && \
echo "Config '${conf}' disabled"
fi
;;
sshshowconf)
for conf in ~/.ssh/config.d/enabled.d/*.conf; do
echo -e "\n ### ${conf##*/} ### "
grep -Ei '^host .*$' "${conf}" | sed -E 's/^[hH]ost (.*)$/\1/g'
done
;;
*)
echo "bad multi-script call name '${EXEC}'"
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment