Preventing overlap with existing ssh configuration hosts.

This commit is contained in:
Kevin Baensch 2023-01-06 09:32:29 +01:00
parent f77e32f7ee
commit f257be1378
2 changed files with 11 additions and 10 deletions

View file

@ -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"

View file

@ -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