Semi working myssh script.

This commit is contained in:
Kevin Baensch 2022-12-22 17:12:23 +01:00
parent 0f8b7b0530
commit b215663184

View file

@ -1,24 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e
if [ -z "$SQL_CLI_TEMPLATE" ] SQL_PROXY_HOST="${SQL_PROXY_HOST:-localhost}"
then CONNECTION_CACHE="$HOME/.cache/sqlproxy_${SQL_PROXY_HOST}"
if [ $(uname -s) = "Linux" ]
then
SQL_CLI_TEMPLATE='mysql --protocol=TCP -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h localhost -P 6033'
else
SQL_CLI_TEMPLATE='open \"mysql://$MYSQL_USERNAME:$MYSQL_PASSWORD@localhost:6033\" -a \"Sequel Ace\"'
fi
else
echo -e "Warning, custom client string:\n$SQL_CLI_TEMPLATE"
read -r -p "Continue [Y/n] " CONTINUE
if [[ ! $CONTINUE =~ "^[Yy]" ]]
then
exit
fi
fi
[ -z $SQL_PROXY_HOST ] && SQL_PROXY_HOST="localhost"
CONNECTION_CACHE="$HOME/.cache/sqlproxy_$SQL_PROXY_HOST"
HELP="Usage: myssh [ls|connect]\n HELP="Usage: myssh [ls|connect]\n
SUBCOMMANDS:\n SUBCOMMANDS:\n
@ -29,6 +12,26 @@ SUBCOMMANDS:\n
SYNTAX connect host [-u user] [-p password] [-c client] SYNTAX connect host [-u user] [-p password] [-c client]
" "
get_template_string() {
if [ -z "$SQL_CLI_TEMPLATE" ]
then
if [ $(uname -s) = "Linux" ]
then
SQL_CLI_TEMPLATE='mysql --protocol=TCP -u $MYSQL_USERNAME -p$MYSQL_PASSWORD -h localhost -P 6033'
else
SQL_CLI_TEMPLATE='open \"mysql://$MYSQL_USERNAME:$MYSQL_PASSWORD@localhost:6033\" -a \"Sequel Ace\"'
fi
else
echo -e "Warning, custom client string:\n$SQL_CLI_TEMPLATE"
read -r -p "Continue [Y/n] " CONTINUE
if [[ ! $CONTINUE =~ "^[Yy]" ]]
then
exit
fi
fi
printf '%s' "${SQL_CLI_TEMPLATE}"
}
ssh_status() { 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 $SQL_PROXY_HOST > /dev/null 2>&1
echo $? echo $?
@ -51,9 +54,13 @@ disconnect() {
fi fi
} }
# Establishes a port forward to the target host
# args:
# $1 - target ip
# $2 - target port
port_forward() { port_forward() {
ACTIVE_HOST=$(cat $CONNECTION_CACHE) ACTIVE_HOST=$(cat $CONNECTION_CACHE)
if [ -z $ACTIVE_HOST ] || [ $ACTIVE_HOST != $1:$2 ] if [ -z $ACTIVE_HOST ] || [ $ACTIVE_HOST != "$1:$2" ]
then then
if [ ! -z $ACTIVE_HOST ] if [ ! -z $ACTIVE_HOST ]
then then
@ -63,7 +70,7 @@ port_forward() {
echo $1 $2 echo $1 $2
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 ${SQL_PROXY_HOST}
fi fi
echo $2:$3 > "${CONNECTION_CACHE}" echo "$1:$2" > "${CONNECTION_CACHE}"
} }
ls_hosts() { ls_hosts() {
@ -71,27 +78,87 @@ ls_hosts() {
} }
get_host() { get_host() {
if [ $1 == '' ] if [ $1 = '' ]
then then
printf 'Please specify the host to connect to.\nRun "myssh ls" to list all available hosts.\n' printf 'Please specify the host to connect to.\nRun "myssh ls" to list all available hosts.\n'
exit 1
else else
ssh -S ${HOME}/.ssh/controlmasters/%r@%h:%p "${SQL_PROXY_HOST}" "get $1" TARGET_HOST_DATA=$(ssh -S ${HOME}/.ssh/controlmasters/%r@%h:%p "${SQL_PROXY_HOST}" "get $1")
if [ "${TARGET_HOST_DATA}" = '' ]
then
printf 'No such host: "%s"\n' $1
exit 1
else
printf "%s" "${TARGET_HOST_DATA}"
fi
fi fi
} }
# Checks and sets sql login variables
# args:
# $1 - sql type (mysql or psql)
# $2 - target ip
# $3 - target port
# $4 - username (optional)
# $5 - password (optional)
set_host_env() { set_host_env() {
if [ $1 == 'mysql' | $1 == 'psql' ] if [ $1 = 'mysql' ] || [ $1 = 'psql' ]
then then
TARGET_HOST_TYPE=$1 TARGET_HOST_TYPE=$1
else
printf 'Invalid Database type: "%s"\n' $1
exit 1
fi fi
if [ $2 != '' ] if [ $2 != '' ]
then then
TARGET_HOST_IP=$2
else
printf 'No Host IP given.\n'
exit 1
fi fi
if echo $3 | grep -q '[0-9]\+'
then
TARGET_HOST_PORT=$3
else
printf 'Invalid Host Port "%s" given.\n' "$3"
fi
if [ -z "${TARGET_HOST_USERNAME}" ] && [ -n $4 ]
then
TARGET_HOST_USERNAME=$4
fi
if [ -z "${TARGET_HOST_PASSWORD}" ] && [ -n $5 ]
then
TARGET_HOST_PASSWORD=$5
fi
} }
# Runs the sql client for an active connection
# args:
# $1 - sql type (mysql or psql)
# $2 - username
# $3 - password
run_client() { run_client() {
$(eval echo $SQL_CLI_TEMPLATE) if [ "$1" = 'mysql' ]
then
if which ace >/dev/null 2>&1
then
ace
elif which mysql >/dev/null 2>&1
then
mysql --protocol=TCP -u "$2" -p"$3" -h "${SQL_PROXY_HOST}" -P 6033
else
SHOW_CLI_HELP=true
fi
elif [ "${$1}" = 'psql' ]
then
SHOW_CLI_HELP=true
fi
if [ "${SHOW_CLI_HELP}" = true ]
then
printf 'No %s client binary found.\nYou can maually establish a connection using the following data.\nhost:\t%s\nport:\t6033\nuser:\t%s\npassword:\t%s\n' "$1" "${SQL_PROXY_HOST}" '3306' "$2" "$3"
fi
} }
MAIN_OPTION=$1 MAIN_OPTION=$1
@ -110,17 +177,16 @@ case $MAIN_OPTION in
while getopts "u:p:" o while getopts "u:p:" o
do do
case "$o" in case "$o" in
u) MYSQL_USERNAME="$OPTARG" ;; u) TARGET_HOST_USERNAME="$OPTARG" ;;
p) MYSQL_PASSWORD="$OPTARG" ;; p) TARGET_HOST_PASSWORD="$OPTARG" ;;
esac esac
done done
TARGET_HOST_DATA="$(get_host "${TARGET_HOST}")" set_host_env $(get_host "${TARGET_HOST}")
port_forward $TARGET_HOST_DATA port_forward "${TARGET_HOST_IP}" "$TARGET_HOST_PORT"
# port_forward $TARGET_HOST if [ -n "${TARGET_HOST_USERNAME}" ] && [ -n "${TARGET_HOST_PASSWORD}" ]
# if [ ! -z $MYSQL_USERNAME ] && [ ! -z $MYSQL_PASSWORD ] then
# then run_client "${TARGET_HOST_TYPE}" "${TARGET_HOST_USERNAME}" "${TARGET_HOST_PASSWORD}"
# run_client fi
# fi
;; ;;
disconnect) disconnect)
disconnect;; disconnect;;