From f257be13782c9adc7ce2c0eba0611d56b5945f8f Mon Sep 17 00:00:00 2001 From: Kevin Baensch Date: Fri, 6 Jan 2023 09:32:29 +0100 Subject: [PATCH] Preventing overlap with existing ssh configuration hosts. --- script/myssh | 15 ++++++++------- setup.sh | 6 +++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/script/myssh b/script/myssh index b656022..1880766 100755 --- a/script/myssh +++ b/script/myssh @@ -1,6 +1,7 @@ #!/usr/bin/env bash set -e SQL_PROXY_HOST="${SQL_PROXY_HOST:-localhost}" +SSH_SQL_PROXY_HOST="sqlproxy.${SQL_PROXY_HOST}" CONNECTION_CACHE="$HOME/.cache/sqlproxy_${SQL_PROXY_HOST}" HELP="Usage: myssh [ls|connect]\n @@ -33,7 +34,7 @@ get_template_string() { } ssh_status() { - ssh -O check -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" > /dev/null 2>&1 + ssh -O check -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SSH_SQL_PROXY_HOST}" > /dev/null 2>&1 } connect() { @@ -42,14 +43,14 @@ connect() { if ! ssh_status then echo "" > "${CONNECTION_CACHE}" - ssh -o "ControlPersist=10m" -M -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" q + ssh -o "ControlPersist=10m" -M -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SSH_SQL_PROXY_HOST}" q fi } disconnect() { if ssh_status then - ssh -O stop -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" q + ssh -O stop -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SSH_SQL_PROXY_HOST}" q fi } @@ -63,15 +64,15 @@ port_forward() { then if [ -n "${ACTIVE_HOST}" ] then - ssh -O cancel -L "6033:${ACTIVE_HOST}" -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" q + ssh -O cancel -L "6033:${ACTIVE_HOST}" -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SSH_SQL_PROXY_HOST}" q fi - ssh -O forward -L "6033:$1:$2" -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" + ssh -O forward -L "6033:$1:$2" -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SSH_SQL_PROXY_HOST}" fi echo "$1:$2" > "${CONNECTION_CACHE}" } ls_hosts() { - ssh -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" ls + ssh -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SSH_SQL_PROXY_HOST}" ls } get_host() { @@ -80,7 +81,7 @@ get_host() { printf 'Please specify the host to connect to.\nRun "myssh ls" to list all available hosts.\n' exit 1 else - TARGET_HOST_DATA=$(ssh -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SQL_PROXY_HOST}" "get $1") + TARGET_HOST_DATA=$(ssh -S "${HOME}/.ssh/controlmasters/%r@%h:%p" "${SSH_SQL_PROXY_HOST}" "get $1") if [ "${TARGET_HOST_DATA}" = '' ] then printf 'No such host: "%s"\n' "$1" diff --git a/setup.sh b/setup.sh index 6cadc98..cdb98f6 100755 --- a/setup.sh +++ b/setup.sh @@ -89,12 +89,12 @@ setup_sqlproxy() { HOST_NAME="${HOST_NAME:-localhost}" # Check if there is an entry for $HOST_NAME in the users ssh config - if ! grep -qe "$(printf '^Host %s$' "${HOST_NAME}")" "${HOME}/.ssh/config" 2>/dev/null + if ! grep -qe "$(printf '^Host %s$' "sqlproxy.${HOST_NAME}")" "${HOME}/.ssh/config" 2>/dev/null then - printf '\nHost %s\n Port 3022\n User sqlproxy\n IdentityFile ~/.ssh/%s' "${HOST_NAME}" "${KEY_NAME}" >> "${HOME}/.ssh/config" + printf '\nHost sqlproxy.%s\n HostName %s\n Port 3022\n User sqlproxy\n IdentityFile ~/.ssh/%s' "${HOST_NAME}" "${HOST_NAME}" "${KEY_NAME}" >> "${HOME}/.ssh/config" else printf 'User ssh configuration located in "%s" already has a configuration for host "%s".\nMake sure your configuration matches the following:\n' "${HOME}/.ssh/config" "${HOST_NAME}" - printf '"""\nHost %s\n Port 3022\n User sqlproxy\n IdentityFile ~/.ssh/%s\n"""\n' "${HOST_NAME}" "${KEY_NAME}" + printf '"""\nHost sqlproxy.%s\n HostName %s\n Port 3022\n User sqlproxy\n IdentityFile ~/.ssh/%s\n"""\n' "${HOST_NAME}" "${HOST_NAME}" "${KEY_NAME}" fi # Check if public key is already in the containers authorized_keys file